轉(zhuǎn)載自?https://blog.csdn.net/qq_35067322/article/details/52811300?locationNum=9&fps=1
https://www.cnblogs.com/zlw-xf/p/8035215.html
[java]?view plaincopy
package?cn.hive.action;????import?org.springframework.stereotype.Controller;??import?org.springframework.web.bind.annotation.PathVariable;??import?org.springframework.web.bind.annotation.RequestMapping;??import?org.springframework.web.bind.annotation.RequestParam;??????????????@Controller??@RequestMapping(value?=?"/{abc}")??public?class?InitAction?{????????????????????????????????@RequestMapping(value?=?"/{init}")??????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)?{??????????abc?=?"hello";??????????init?=?"world";??????????System.out.println(name?+?age);??????????return?"test";??????}????}??
測試頁面? index.jsp
[html]?view plaincopy
<%@?taglib?prefix="c"?uri="http://java.sun.com/jsp/jstl/core"?%>??<%--????Created?by?IntelliJ?IDEA.????User:?Administrator????Date:?2016/10/13????Time:?16:34????To?change?this?template?use?File?|?Settings?|?File?Templates.??--%>??<%@?page?contentType="text/html;charset=UTF-8"?language="java"?%>??<%??????String?path?=?request.getContextPath();??????String?basePath?=?request.getScheme()?+?"://"?+?request.getServerName()?+?":"?+?request.getServerPort()?+?path?+?"/";??%>??<html>??<head>??????<title>Title</title>??</head>??<body>??<form?action="<c:url?value="/hello/world.action"/>"?method="post"?>??????<label>??????????<input?type="text"?name="name">??????????<input?type="text"?name="age">??????</label>??????<input?type="submit"?value="提交">??</form>??<a?href="<c:url?value="/hello/world.action"/>">test</a>??</body>??</html>??
成功頁面
[html]?view plaincopy
<%--????Created?by?IntelliJ?IDEA.????User:?felord????Date:?2016/10/13????Time:?20:21????To?change?this?template?use?File?|?Settings?|?File?Templates.??--%>??<%@?page?contentType="text/html;charset=UTF-8"?language="java"?%>??<html>??<head>??????<title>Title</title>??</head>??<body>??aaaaaaaaaaaaa??${param.name}??${param.age}??bbbbbbbbbbbb??${param.id}????</body>??</html>??
對于 params? 已經(jīng)解釋過了 因?yàn)?有沖突? 沒有測試代碼? ? 可自行測試 ???
---------------------------------------------------------------------------------------------------------------------------------------------
@PathVariable綁定URI模板變量值
@PathVariable是用來獲得請求url中的動態(tài)參數(shù)的
@PathVariable用于將請求URL中的模板變量映射到功能處理方法的參數(shù)上。//配置url和方法的一個關(guān)系@RequestMapping("item/{itemId}")
/* @RequestMapping 來映射請求,也就是通過它來指定控制器可以處理哪些URL請求,類似于struts的action請求
* @responsebody表示該方法的返回結(jié)果直接寫入HTTP response body中
*一般在異步獲取數(shù)據(jù)時使用,在使用@RequestMapping后,返回值通常解析為跳轉(zhuǎn)路徑,加上@responsebody后返回結(jié)果不會被解析為跳轉(zhuǎn)路徑,而是直接寫入HTTP response *body中。
*比如異步獲取json數(shù)據(jù),加上@responsebody后,會直接返回json數(shù)據(jù)。*
*@Pathvariable注解綁定它傳過來的值到方法的參數(shù)上
*用于將請求URL中的模板變量映射到功能處理方法的參數(shù)上,即取出uri模板中的變量作為參數(shù)
*/
@ResponseBody
public TbItem getItemById(@PathVariable Long itemId){
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("參數(shù)錯誤"
);
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 }
在SpringMVC后臺控制層獲取參數(shù)的方式主要有兩種:
一種是request.getParameter("name"),另外一種是用注解@RequestParam直接獲取
這里主要講這個注解?@RequestParam
接下來我們看一下@RequestParam注解主要有哪些參數(shù):
value:參數(shù)名字,即入?yún)⒌恼埱髤?shù)名字,如username表示請求的參數(shù)區(qū)中的名字為username的參數(shù)的值將傳入;
required:是否必須,默認(rèn)是true,表示請求中一定要有相應(yīng)的參數(shù),否則將報404錯誤碼;
defaultValue:默認(rèn)值,表示如果請求中沒有同名參數(shù)時的默認(rèn)值,例如:
public List<EasyUITreeNode> getItemTreeNode(@RequestParam(value="id",defaultValue="0")long parentId)
?
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 }
總結(jié)
以上是生活随笔為你收集整理的springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。