一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring MVC 學習 之 - URL參數傳遞詳解

Spring MVC 學習 之 - URL參數傳遞詳解

2020-07-23 12:56Rhythmk Java教程

本篇文章主要介紹了SpringMVC-URL參數傳遞,在學習 Spring Mvc 過程中,有必要來先了解幾個關鍵參數,有興趣的可以了解一下。

在學習 Spring Mvc 過程中,有必要來先了解幾個關鍵參數:

@Controller:

在類上注解,則此類將編程一個控制器,在項目啟動 Spring 將自動掃描此類,并進行對應URL路由映射。

?
1
2
3
@Controller
 
public class UserAction{ }

@RequestMapping

指定URL映射路徑,如果在控制器上配置 RequestMapping  ,具體請求方法也配置路徑則映射的路徑為兩者路徑的疊加 常用映射如:RequestMapping("url.html")

配置映射路徑:

?
1
2
3
4
5
6
7
8
@Controller
public class UserAction
{
  @RequestMapping(value = "/get_alluser.html")
  public ModelAndView GetAllUser(String Id)
  {
  }
}

以上配置映射

http://***:8080:web1/get_alluser.html:

如在 @Controller添加 @RequestMapping(value = "/user"),則映射路徑變成

http://***:8080:web1/user/get_alluser.html

@ResponseBody

將注解方法對應的字符串直接返回

 @RequestParam

自動映射URL對應的參數到Action上面的數值,RequestParam 默認為必填參數。

 @PathVariable

獲取@RequestMapping 配置指定格式的URL映射參數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
  *  直接輸出 HTML,或JSON 字符串
  *  請求路徑:
  *    /web1/urlinfo/getcontent.html?key=rhythmk
  *   /web1/urlinfo/getcontent.json?key=rhythmk
  * */
 @ResponseBody
 @RequestMapping(value = "/getcontent.**")
 public String GetContent(
     @RequestParam("key") String key,
     @RequestParam(value = "key2", required = false, defaultValue = "defaultValue") String key2) {
   System.out.println("getcontent 被調用");
   String result = "直接返回內容 - key:" + key + ",key2:" + key2;
   System.out.println(result);
   return result;
 }
?
1
2
3
4
5
6
7
8
9
10
11
/*
  * RequestMapping 支持 Ant 風格的URL配置 :
  * 請求路徑:
  *   /urlinfo/geturlant/config.html?key=adddd
  */
 @ResponseBody
 @RequestMapping(value = "/geturlant/**.html")
 public String getUrlAnt(HttpServletRequest request) {
   String result = "?后面的參數為:" + request.getQueryString();
   return result;
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
  * 配置指定格式的URL,映射到對應的參數
  *  請求路徑:/web1/urlinfo/geturlparam/12_123.html
  *  
  * */
 
 @RequestMapping(value = "/geturlparam/{id}_{menuId}.html")
 public ModelAndView getUrlParam(@PathVariable("id") String id,
     @PathVariable("menuId") String menuId) {
   ModelAndView mode = new ModelAndView(ShowMsg);
   mode.addObject("msg", "獲取到的Id:" + id + ",menuId:" + menuId);
   return mode;
 }
?
1
2
3
4
5
6
7
8
/*
 * 只接收Post 請求
 */
@ResponseBody
@RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
public String UrlMethod(@RequestParam String id) {
  return "只能是Post請求,獲取到的Id:" + id;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
/*
 *  寫入 cookie
 * */
@RequestMapping("/writecookies.html")
public ModelAndView writeCookies(@RequestParam String value,
    HttpServletResponse response) {
 
  response.addCookie(new Cookie("key", value));
  ModelAndView mode = new ModelAndView(ShowMsg);
  mode.addObject("msg", "cookies 寫入成功");
  return mode ;
}
?
1
2
3
4
5
6
7
8
9
/*
  * 通過 @CookieValue 獲取對應的key的值
  * */
@RequestMapping("/getcookies.html")
public ModelAndView getCookie(@CookieValue("key") String cookvalue) {
  ModelAndView mode = new ModelAndView(ShowMsg);
  mode.addObject("msg", "cookies=" + cookvalue);
  return mode;
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * 將 Servlet Api 作為參數傳入
 *  可以在action中直接使用 HttpServletResponse,HttpServletRequest
 * */
@RequestMapping("/servlet.html")
public String Servlet1(HttpServletResponse response,
    HttpServletRequest request) {
 
  Boolean result = (request != null && response != null);
  ModelAndView mode = new ModelAndView();
  mode.addObject("msg", "result=" + result.toString());
  return ShowMsg;
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
 *  根據URL傳入的參數實例化對象
 
 *  如: http://127.0.0.1:8080/web1/urlinfo/getobject.html?UserId=1&UserName=ad
 * */
@RequestMapping("getobject.html")
public ModelAndView getObject(UserInfo user) {
  String result = "用戶ID:" + user.getUserId().toString() + ",用戶名:"
      + user.getUserName().toString();
  ModelAndView mode = new ModelAndView(ShowMsg);
  mode.addObject("msg", "result=" + result.toString());
  return mode;
}

 實現頁面跳轉:

?
1
2
3
4
5
6
7
8
9
10
/*
 * 實現頁面跳轉
 * /web1/urlinfo/redirectpage.html
 * */
@RequestMapping("/redirectpage.html")
public String RedirectPage()
{
  return "redirect:getcookies.html?r=10";
      
}

直接回傳JSON

請求的URL地址一定是以.json結尾,否則異常

Failed to load resource: the server responded with a status of 406 (Not Acceptable) : The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers () 

回傳實體:
 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class UserInfo {
 
   private Integer UserId;
   public Integer getUserId() {
    return UserId;
  }
  public void setUserId(Integer userId) {
    UserId = userId;
  }
  public String getUserName() {
    return UserName;
  }
  public void setUserName(String userName) {
    UserName = userName;
  }
  private String UserName;
   
   
}

回傳 action 

?
1
2
3
4
5
6
7
8
9
10
@ResponseBody
  @RequestMapping("/getuser.json")
  public UserInfo GetUser()
  {
    System.out.println("getuser");
    UserInfo model=new UserInfo();
    model.setUserId(100);
    model.setUserName("王坤");
    return model;
  }

請求:

/web1/urlinfo/getuser.json

輸出:

?
1
{"userId":100,"userName":"王坤"}

 以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 朝鲜女人free性hu | 精精国产xxxx视频在线播放器 | 门房秦大爷在线阅读 | 西野翔全部作品在线观看 | 青青青在线视频 | 亚洲AV无码乱码国产麻豆穿越 | 毛片 ftp| 国模孕妇季玥337p人体 | 99精品在线视频观看 | 亚洲精品无码久久不卡 | 美女天天色 | 男人狂躁女人下半身 | 国产成人高清视频 | 国产一区二区三区四卡 | 日本暖暖在线视频 | a级黄色片免费 | 99精品在线视频观看 | 欧美视频在线一区二区三区 | 欧美日韩一区二区三区久久 | 2022国产麻豆剧传媒剧情 | 国产精品拍拍拍福利在线观看 | 成年人在线视频免费观看 | 亚洲欧美日韩国产精品一区 | 亚洲成人99 | 精品高潮呻吟99AV无码视频 | 色婷婷综合缴情综六月 | 狠狠干2016| 亚洲视频在线观看地址 | 青青国产成人久久91网 | 成年人在线视频观看 | 高h折磨调教古代 | 亚洲天堂成人在线 | 亚洲国产成人久久综合区 | 免费午夜剧场 | 99r8这里精品热视频免费看 | 特大黑人娇小亚洲女mp4 | 潘甜甜在线观看 | 国产一区二区精品久 | 日韩精品视频在线观看免费 | 国产欧美日韩成人 | 久久亚洲精品专区蓝色区 |