日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解

發(fā)布時間:2024/2/28 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springMVC注解中@RequestMapping中常用参数value params 以及@RequestParam 详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(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;??
  • ??
  • /**?
  • ?*?Created?with?IntelliJ?IDEA.?
  • ?*?Author:?DAX?
  • ?*?Date:?2016/10/13?
  • ?*?測試action類??
  • ?*?Time:?20:08?
  • ?*/??
  • ??
  • @Controller??
  • @RequestMapping(value?=?"/{abc}")??
  • public?class?InitAction?{??
  • ????/*?
  • ?????*??@RequestMapping??value??和params?的詳解?
  • ?????*?
  • ?????*?
  • ?????*?如類沒有定義請求映射?類方法中的value代表根路徑??如果在類方法中有點(diǎn)類似于struts中?action的id?
  • ?????*?params?為請求參數(shù)的數(shù)組?支持一些簡單的表達(dá)式??????params={"!id","name!=James"}??表示不能帶名稱為id的參數(shù)??而且name的值不能為James??等等表達(dá)式????
  • ?????*?
  • ?????*?@RequestMapping(value?=?"/init",?params?=?{"id=myValue"})?只有存在了請求參數(shù)id=myValue??/init.action?id=myValue?才會被initData處理?
  • ?????*?@RequestMapping(value?=?"/init",?params?=?{"name=kobe",?"number=23"})?/init.action?name=kobe&&number=23?否則?404錯誤?
  • ?????*?
  • ?????*?一旦abc??init??為占位符即用{}包括起來?該請求默認(rèn)為下面?
  • ?????*?http://localhost:8080/abc/init.action?
  • ?????*?如果被賦值??例如??abc?=?"hello";???init?=?"world";??則下面網(wǎng)址也可以訪問ininData方法?
  • ?????*?http://localhost:8080/hello/world.action?
  • ?????*?這形成了具有REST(表現(xiàn)層狀態(tài)轉(zhuǎn)化)風(fēng)格的請求形式??表示?abc?的id為?init的實(shí)際賦值?但是請求的方法必須為GET?
  • ?????*?
  • ?????*?@RequestParam?詳解??接收?請求參數(shù)?
  • ?????*?required參數(shù)默認(rèn)為false???表示???可以為空?
  • ?????*?如果為?數(shù)據(jù)的基本類型?????一旦沒有賦值??提交??會被賦值null?
  • ?????*?拋出異常?一般推薦用包裝類?來接收??比如??int??用?Integer???double??用Double??等?
  • ?????*/??
  • ????@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)容還不錯,歡迎將生活随笔推薦給好友。