欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring MVC 學(xué)習(xí) 之 - URL參數(shù)傳遞詳解

 更新時(shí)間:2017年01月07日 17:23:20   作者:Rhythmk  
本篇文章主要介紹了SpringMVC-URL參數(shù)傳遞,在學(xué)習(xí) Spring Mvc 過程中,有必要來先了解幾個(gè)關(guān)鍵參數(shù),有興趣的可以了解一下。

在學(xué)習(xí) Spring Mvc 過程中,有必要來先了解幾個(gè)關(guān)鍵參數(shù):

@Controller:

在類上注解,則此類將編程一個(gè)控制器,在項(xiàng)目啟動(dòng) Spring 將自動(dòng)掃描此類,并進(jìn)行對應(yīng)URL路由映射。

@Controller

public class UserAction{ } 

@RequestMapping

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

配置映射路徑:

@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

將注解方法對應(yīng)的字符串直接返回

 @RequestParam

自動(dòng)映射URL對應(yīng)的參數(shù)到Action上面的數(shù)值,RequestParam 默認(rèn)為必填參數(shù)。

 @PathVariable

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

 /*
   *  直接輸出 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 被調(diào)用");
    String result = "直接返回內(nèi)容 - key:" + key + ",key2:" + key2;
    System.out.println(result);
    return result;
  } 

 /*
   * RequestMapping 支持 Ant 風(fēng)格的URL配置 :
   * 請求路徑:
   *   /urlinfo/geturlant/config.html?key=adddd
   */
  @ResponseBody
  @RequestMapping(value = "/geturlant/**.html")
  public String getUrlAnt(HttpServletRequest request) {
    String result = "?后面的參數(shù)為:" + request.getQueryString();
    return result;
  } 
 /*
   * 配置指定格式的URL,映射到對應(yīng)的參數(shù)
   *  請求路徑:/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;
  } 
  /*
   * 只接收Post 請求
   */
  @ResponseBody
  @RequestMapping(value = "/posturl.html", method = RequestMethod.POST)
  public String UrlMethod(@RequestParam String id) {
    return "只能是Post請求,獲取到的Id:" + id;
  } 
  /*
   *  寫入 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 ;
  } 

  /*
    * 通過 @CookieValue 獲取對應(yīng)的key的值
    * */
  @RequestMapping("/getcookies.html")
  public ModelAndView getCookie(@CookieValue("key") String cookvalue) {
    ModelAndView mode = new ModelAndView(ShowMsg);
    mode.addObject("msg", "cookies=" + cookvalue);
    return mode;
  } 
  /* 
   * 將 Servlet Api 作為參數(shù)傳入 
   *  可以在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;

  } 
  /*
   *  根據(jù)URL傳入的參數(shù)實(shí)例化對象
   *  
   *  如: 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;
  } 

 實(shí)現(xiàn)頁面跳轉(zhuǎn):

  /* 
   * 實(shí)現(xiàn)頁面跳轉(zhuǎn)
   * /web1/urlinfo/redirectpage.html
   * */
  @RequestMapping("/redirectpage.html")
  public String RedirectPage()
  {
    return "redirect:getcookies.html?r=10"; 
        
  } 

直接回傳JSON

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

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 () 

回傳實(shí)體:
 

@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 

@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

輸出:

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

 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java web的讀取Excel簡單實(shí)例代碼

    Java web的讀取Excel簡單實(shí)例代碼

    下面小編就為大家?guī)硪黄狫ava web的讀取Excel簡單實(shí)例代碼。小編覺挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-06-06
  • 解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問題

    解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問題

    最近在創(chuàng)建了SpringBoot項(xiàng)目后往配置文件中寫了相關(guān)的系統(tǒng)配置,并且在上面加了中文注釋,但是在重啟項(xiàng)目或開機(jī)重啟后遇到了注釋亂碼的情況,下面這篇文章主要給大家介紹一下如何解決SpringBoot配置文件項(xiàng)目重啟出現(xiàn)亂碼的問題,需要的朋友可以參考下
    2023-06-06
  • Springboot項(xiàng)目異常處理及返回結(jié)果統(tǒng)一

    Springboot項(xiàng)目異常處理及返回結(jié)果統(tǒng)一

    這篇文章主要介紹了Springboot項(xiàng)目異常處理及返回結(jié)果統(tǒng)一,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • Java大數(shù)字運(yùn)算之BigInteger

    Java大數(shù)字運(yùn)算之BigInteger

    在Java中提供了大數(shù)字的操作類,即 java.math.BigInteger 類與 java.math.BigDecimal 類。其中,BigInteger 類是針對大整數(shù)的處理類,這里有Integer 類的解釋,使用方法和實(shí)例,需要的朋友可以參考下。
    2017-08-08
  • Java 中 Reference用法詳解

    Java 中 Reference用法詳解

    這篇文章主要介紹了Java 中 Reference用法詳解的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Springboot之如何統(tǒng)計(jì)代碼執(zhí)行耗時(shí)時(shí)間

    Springboot之如何統(tǒng)計(jì)代碼執(zhí)行耗時(shí)時(shí)間

    這篇文章主要介紹了Springboot之如何統(tǒng)計(jì)代碼執(zhí)行耗時(shí)時(shí)間問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Flink JobGraph生成源碼解析

    Flink JobGraph生成源碼解析

    這篇文章主要為大家介紹了Flink JobGraph生成源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • SpringBoot手寫自定義starter源碼

    SpringBoot手寫自定義starter源碼

    這篇文章主要介紹了SpringBoot手寫自定義starter源碼,SpringBoot擁有很多方便使用的starter,比如spring-boot-starter-log4j、mybatis-spring-boot-starter.jar等,各自都代表了一個(gè)相對完整的功能模塊,需要的朋友可以參考下
    2023-10-10
  • 將本地SpringBoot項(xiàng)目發(fā)布到云服務(wù)器的方法

    將本地SpringBoot項(xiàng)目發(fā)布到云服務(wù)器的方法

    這篇文章主要介紹了如何將本地SpringBoot項(xiàng)目發(fā)布到云服務(wù)器,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • hibernate和mybatis對比分析

    hibernate和mybatis對比分析

    通過本文給分享了hibernate和mybatis對比分析,從開發(fā)對比,系統(tǒng)調(diào)優(yōu)對比,對象管理與抓取策略,緩存機(jī)制對比等方面給大家詳細(xì)介紹,需要的朋友參考下吧
    2017-09-09

最新評(píng)論