SpringMVC 中的注解@RequestParam与@PathVariable的区别
@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的区别的更多相关文章
- springMVC中的注解@RequestParam与@PathVariable的区别
1.@PathVariable @PathVariable绑定URI模板变量值 @PathVariable是用来获得请求url中的动态参数的 @PathVariable用于将请求URL中的模板变量映射 ...
- springMVC的注解@RequestParam与@PathVariable的区别
1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...
- SSM-SpringMVC-14:SpringMVC中大话注解式开发基础--呕心沥血版
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解的基础我不再多啰嗦,百度一搜很多,很详细啊,我就讲一下SpringMVC中的注解入门 通过注解的方式定义 ...
- SpringMVC 中 @ControllerAdvice 注解
SpringMVC 中 @ControllerAdvice 注解 1.@ControllerAdvice 1.1 全局异常处理 1.2 全局数据绑定 1.3 全局数据预处理 原文地址: 江南一点雨:S ...
- @RequestParam 和 @ PathVariable 的区别
@RequestParam 和 @ PathVariable 的区别http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNo ...
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping的区别
SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别 @Controller 处理http请求 @Controller //@Re ...
- Spring|SpringMVC中的注解
文章目录 一.Spring注解 @Controller @ResuController @Service @Autowired @RequestMapping @RequestParam @Model ...
- springmvc中ModelAttribute注解应用在参数中
可以用@ModelAttribute来注解方法参数或方法.带@ModelAttribute创建的参数对象会被添加到Model对象中.注解在参数上时,可以从Form表单或URL参数中获取参数并绑定到mo ...
- SpringBoot中常用注解@Controller/@RestController/@RequestMapping的区别
@Controller 处理http请求 @Controller //@ResponseBody public class HelloController { @RequestMapping(valu ...
随机推荐
- vsftp软件安装部署
1.安装vsftp yum install -y vsftpd db4-utils2.默认可以支持系统用户账号远程登录.不安全,建立虚拟账号体系为好.或者在服务器端对vsftpd.conf配置文件进行 ...
- VirtualBox-5.2.8-121009-Win.exe虚拟机桥接
- miniui 请求遮罩mask不生效
通用写法: mini.mask({ el:document.body(作用对象) cls:样式 hetml:显示文本 }) 我的问题是,当前页面的遮罩无效不会显示,但是页面不能点击 问题原因,aj ...
- word2vec原理浅析
1.word2vec简介 word2vec,即词向量,就是一个词用一个向量来表示.是2013年Google提出的.word2vec工具主要包含两个模型:跳字模型(skip-gram)和连续词袋模型( ...
- 【神经网络与深度学习】【Matlab开发】caffe-windows使能Matlab2015b接口
[神经网络与深度学习][Matlab开发]caffe-windows使能Matlab2015b接口 标签:[神经网络与深度学习] [Matlab开发] 主要是想全部来一次,所以使能了Matlab的接口 ...
- Mac022-brew安装tool
1.Mac上查看目录的树结构 Step1:安装tree命令 brew install tree Step2:某一目录下执行tree,会将该目录下的所有目录以树形结构显示 $ tree $ tree - ...
- javascrpt的string和Boolean类型
string类型用于表示由零或多个16位unicode字符组成字符序列,即 字符串,字符可以由双引号(“)或单引号(‘)表示 tostring()与 string() 语法:str.tostring( ...
- Akka系列(十):Akka集群之Akka Cluster
前言........... 上一篇文章我们讲了Akka Remote,理解了Akka中的远程通信,其实Akka Cluster可以看成Akka Remote的扩展,由原来的两点变成由多点组成的通信网络 ...
- Qt - 基于TCP的网络编程
TCP(传输控制协议 Transmission Control Protocol) 可靠.面向数据流.面向连接 的传输协议.(许多应用层协议都是以它为基础:HTTP.FTP) 使用情况: 相比UDP ...
- HDU 1176 免费馅饼 (动态规划、另类数塔)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...