springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解 柔光的暖阳◎ 2022-05-23 11:39 160阅读 0赞 转载自 https://blog.csdn.net/qq\_35067322/article/details/52811300?locationNum=9&fps=1 https://www.cnblogs.com/zlw-xf/p/8035215.html \[java\] [view plain][] [copy][view plain] 1. **package** cn.hive.action; 2. 3. **import** org.springframework.stereotype.Controller; 4. **import** org.springframework.web.bind.annotation.PathVariable; 5. **import** org.springframework.web.bind.annotation.RequestMapping; 6. **import** org.springframework.web.bind.annotation.RequestParam; 7. 8. /\*\* 9. \* Created with IntelliJ IDEA. 10. \* Author: DAX 11. \* Date: 2016/10/13 12. \* 测试action类 13. \* Time: 20:08 14. \*/ 15. 16. @Controller 17. @RequestMapping(value = "/\{abc\}") 18. **public** **class** InitAction \{ 19. /\* 20. \* @RequestMapping value 和params 的详解 21. \* 22. \* 23. \* 如类没有定义请求映射 类方法中的value代表根路径 如果在类方法中有点类似于struts中 action的id 24. \* params 为请求参数的数组 支持一些简单的表达式 params=\{"!id","name!=James"\} 表示不能带名称为id的参数 而且name的值不能为James 等等表达式 25. \* 26. \* @RequestMapping(value = "/init", params = \{"id=myValue"\}) 只有存在了请求参数id=myValue /init.action?id=myValue 才会被initData处理 27. \* @RequestMapping(value = "/init", params = \{"name=kobe", "number=23"\}) /init.action?name=kobe&&number=23 否则 404错误 28. \* 29. \* 一旦abc init 为占位符即用{}包括起来 该请求默认为下面 30. \* http://localhost:8080/abc/init.action 31. \* 如果被赋值 例如 abc = "hello"; init = "world"; 则下面网址也可以访问ininData方法 32. \* http://localhost:8080/hello/world.action 33. \* 这形成了具有REST(表现层状态转化)风格的请求形式 表示 abc 的id为 init的实际赋值 但是请求的方法必须为GET 34. \* 35. \* @RequestParam 详解 接收 请求参数 36. \* required参数默认为false 表示 可以为空 37. \* 如果为 数据的基本类型 一旦没有赋值 提交 会被赋值null 38. \* 抛出异常 一般推荐用包装类 来接收 比如 int 用 Integer double 用Double 等 39. \*/ 40. @RequestMapping(value = "/\{init\}") 41. **public** String initData(@PathVariable("abc") String abc, @PathVariable("init") String init, @RequestParam(value = "name", required = **false**) String name, @RequestParam(value = "age", required = **false**) Integer age) \{ 42. abc = "hello"; 43. init = "world"; 44. System.out.println(name + age); 45. **return** "test"; 46. \} 47. 48. \} 测试页面 index.jsp \[html\] [view plain][] [copy][view plain] 1. **<**%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %**>** 2. **<**%-- 3. Created by IntelliJ IDEA. 4. User: Administrator 5. Date: 2016/10/13 6. Time: 16:34 7. To change this template use File | Settings | File Templates. 8. \--%**>** 9. **<**%@ page contentType="text/html;charset=UTF-8" language="java" %**>** 10. **<**% 11. String path = request.getContextPath(); 12. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; 13. %**>** 14. **<****html****>** 15. **<****head****>** 16. **<****title****>**Title**</****title****>** 17. **</****head****>** 18. **<****body****>** 19. **<****form** action="<c:url value="/hello/world.action"**/>**" method="post" **>** 20. **<****label****>** 21. **<****input** type="text" name="name"**>** 22. **<****input** type="text" name="age"**>** 23. **</****label****>** 24. **<****input** type="submit" value="提交"**>** 25. **</****form****>** 26. **<****a** href="<c:url value="/hello/world.action"**/>**"**>**test**</****a****>** 27. **</****body****>** 28. **</****html****>** 成功页面 \[html\] [view plain][] [copy][view plain] 1. **<**%-- 2. Created by IntelliJ IDEA. 3. User: felord 4. Date: 2016/10/13 5. Time: 20:21 6. To change this template use File | Settings | File Templates. 7. \--%**>** 8. **<**%@ page contentType="text/html;charset=UTF-8" language="java" %**>** 9. **<****html****>** 10. **<****head****>** 11. **<****title****>**Title**</****title****>** 12. **</****head****>** 13. **<****body****>** 14. aaaaaaaaaaaaa 15. $\{param.name\} 16. $\{param.age\} 17. bbbbbbbbbbbb 18. $\{param.id\} 19. 20. **</****body****>** 21. **</****html****>** 对于 params 已经解释过了 因为 有冲突 没有测试代码 可自行测试 \--------------------------------------------------------------------------------------------------------------------------------------------- @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)\{ ![复制代码][copycode.gif] 1 @RequestMapping("/zyh/{type}") 2 public String zyh(@PathVariable(value = "type") int type) throws UnsupportedEncodingException { 3 String url = "http://wx.diyfintech.com/zyhMain/" + type; 4 if (type != 1 && type != 2) { 5 throw new IllegalArgumentException("参数错误"); 6 } 7 String encodeUrl = URLEncoder.encode(url, "utf-8"); 8 String redirectUrl = MessageFormat.format(OAUTH_URL, WxConfig.zyhAppId, encodeUrl, "snsapi_userinfo", UUID.randomUUID().toString().replace("-", "")); 9 return "redirect:" + redirectUrl; 10 } ![复制代码][copycode.gif] 在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) ![复制代码][copycode.gif] 1 @Controller 2 @RequestMapping("/wx") 3 public class WxController { 4 5 @Autowired 6 private WxService wxService; 7 private static final Log log= LogFactory.getLog(WxController.class); 8 9 @RequestMapping(value = "/service",method = RequestMethod.GET) 10 public void acceptWxValid(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce, 11 @RequestParam String echostr, HttpServletResponse response) throws IOException { 12 PrintWriter out = response.getWriter(); 13 if (SignUtil.checkSignature(signature, timestamp, nonce)) { 14 out.print(echostr); 15 }else 16 out.print("fail"); 17 out.flush(); 18 out.close(); 19 } ![复制代码][copycode.gif] [view plain]: https://blog.csdn.net/qq_35067322/article/details/52811300?locationNum=9&fps=1# [copycode.gif]: /images/20220523/775a3ea7fdb3437b95dc8a8262c71e13.png
还没有评论,来说两句吧...