@PathVariable绑定URI模板变量值

@PathVariable是用来获得请求url中的动态参数的

@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。//配置url和方法的一个关系@RequestMapping("item/{itemId}")

/* @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,类似于struts的action请求
* @responsebody表示该方法的返回结果直接写入HTTP response body中
*一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response *body中。
*比如异步获取json数据,加上@responsebody后,会直接返回json数据。*
*@Pathvariable注解绑定它传过来的值到方法的参数上
*用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数
*/
@ResponseBody
public TbItem getItemById(@PathVariable Long itemId){

@RequestMapping("/zyh/{type}")
public String zyh(@PathVariable(value = "type") int type) throws UnsupportedEncodingException {
String url = "http://wx.diyfintech.com/zyhMain/" + type;
if (type != 1 && type != 2) {
throw new IllegalArgumentException("参数错误");
}
String encodeUrl = URLEncoder.encode(url, "utf-8");
String redirectUrl = MessageFormat.format(OAUTH_URL, WxConfig.zyhAppId, encodeUrl, "snsapi_userinfo", UUID.randomUUID().toString().replace("-", ""));
return "redirect:" + redirectUrl;
}

在SpringMVC后台控制层获取参数的方式主要有两种:

一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取

这里主要讲这个注解 @RequestParam

接下来我们看一下@RequestParam注解主要有哪些参数:

value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;

required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;

defaultValue:默认值,表示如果请求中没有同名参数时的默认值,例如:

public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0")long parentId)

@Controller
@RequestMapping("/wx")
public class WxController { @Autowired
private WxService wxService;
private static final Log log= LogFactory.getLog(WxController.class); @RequestMapping(value = "/service",method = RequestMethod.GET)
public void acceptWxValid(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce,
@RequestParam String echostr, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
if (SignUtil.checkSignature(signature, timestamp, nonce)) {
out.print(echostr);
}else
out.print("fail");
out.flush();
out.close();
}

本文转自:https://www.cnblogs.com/zlw-xf/p/8035215.html

SpringMVC 中的注解@RequestParam与@PathVariable的区别的更多相关文章

  1. springMVC中的注解@RequestParam与@PathVariable的区别

    1.@PathVariable @PathVariable绑定URI模板变量值 @PathVariable是用来获得请求url中的动态参数的 @PathVariable用于将请求URL中的模板变量映射 ...

  2. springMVC的注解@RequestParam与@PathVariable的区别

    1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...

  3. SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版

     ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...

  4. SpringMVC 中 @ControllerAdvice 注解

    SpringMVC 中 @ControllerAdvice 注解 1.@ControllerAdvice 1.1 全局异常处理 1.2 全局数据绑定 1.3 全局数据预处理 原文地址: 江南一点雨:S ...

  5. @RequestParam 和 @ PathVariable 的区别

    @RequestParam 和 @ PathVariable 的区别http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNo ...

  6. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别

    SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...

  7. Spring|SpringMVC中的注解

    文章目录 一.Spring注解 @Controller @ResuController @Service @Autowired @RequestMapping @RequestParam @Model ...

  8. springmvc中ModelAttribute注解应用在参数中

    可以用@ModelAttribute来注解方法参数或方法.带@ModelAttribute创建的参数对象会被添加到Model对象中.注解在参数上时,可以从Form表单或URL参数中获取参数并绑定到mo ...

  9. SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别

    @Controller 处理http请求 @Controller //@ResponseBody public class HelloController { @RequestMapping(valu ...

随机推荐

  1. 【翻译】WPF应用程序模块化开发快速入门(使用Prism+MEF)

    编译并运行快速入门 需要在VisualStudio 2010上运行此快速入门示例 代码下载:ModularityWithMef.zip 先重新生成解决方案 再按F5运行此示例 说明: 在此快速入门示例 ...

  2. Appium+python自动化-输入中文

    一.定位搜索 1.打开淘宝点击搜索按钮,进入到搜索页面 2.然后定位到搜索框后用sendkeys方法输入‘hao’,这里定位元素使用uiautomatorviewer工具即可 3.脚本如下,输入的是英 ...

  3. 阶段3 2.Spring_01.Spring框架简介_03.spring概述

  4. 物料批量盘点,调用其中两个BAPI BAPI_MATPHYSINV_COUNT BAPI_MATPHYSINV_CHANGECOUNT

    涉及两个BAPI:录入数量BAPI_MATPHYSINV_COUNT 修改数量:BAPI_MATPHYSINV_CHANGECOUNT REPORT  ZSC_133 NO STANDARD PAGE ...

  5. gitlab中的CI

    https://blog.csdn.net/chengzi_comm/article/details/78778284

  6. java:Hibernate框架1(环境搭建,Hibernate.cfg.xml中属性含义,Hibernate常用API对象,HibernteUitl,对象生命周期图,数据对象的三种状态,增删查改)

    1.环境搭建: 三个准备+7个步骤 准备1:新建项目并添加hibernate依赖的jar文件  准备2:在classpath下(src目录下)新建hibernate的配置文件:hibernate.cf ...

  7. [转帖]SUN/Oracle JDK还是OpenJDK?

    你安装的是 https://www.cnblogs.com/shoufeng/p/9719995.html 目录 1 如何查看你安装的JDK版本 1.1 要用到的命令行工具 1.2 查看JDK的版本 ...

  8. [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)

    [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...

  9. Gym 101466(完整)

    题目链接 :点击此处 ## Problem A 题意: 给你n个数,重定义两个数之间的加法不进位,求这些数中两个数相加的最大值和最小值. 题解: 字典树.我们首先将前i-1为放入字典树中,然后在查询第 ...

  10. Quartz.net 3.x使用总结(一)——简单使用

    原文:Quartz.net 3.x使用总结(一)--简单使用 阅读目录 1.Quartz.net简介 2.简单使用 3.TriggerBuilder介绍 3.1  SimpleSchedule 3.2 ...