Java Spring Controller 擷取請求參數的幾種方法詳解_java

來源:互聯網
上載者:User

Java Spring Controller 擷取請求參數的幾種方法

 1、直接把表單的參數寫在Controller相應的方法的形參中,適用於get方式提交,不適用於post方式提交。若"Content-Type"="application/x-www-form-urlencoded",可用post提交

       url形式:http://localhost:8080/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 提交的參數需要和Controller方法中的入參名稱一致。  

 /**   * 1.直接把表單的參數寫在Controller相應的方法的形參中   * @param username   * @param password   * @return   */  @RequestMapping("/addUser1")  public String addUser1(String username,String password) {    System.out.println("username is:"+username);    System.out.println("password is:"+password);    return "demo/index";  }

2、通過HttpServletRequest接收,post方式和get方式都可以。

 /**   * 2、通過HttpServletRequest接收   * @param request   * @return   */  @RequestMapping("/addUser2")  public String addUser2(HttpServletRequest request) {    String username=request.getParameter("username");    String password=request.getParameter("password");    System.out.println("username is:"+username);    System.out.println("password is:"+password);    return "demo/index";  }

3、通過一個bean來接收,post方式和get方式都可以。

  /**   * 3、通過一個bean來接收   * @param user   * @return   */  @RequestMapping("/addUser3")  public String addUser3(UserModel user) {    System.out.println("username is:"+user.getUsername());    System.out.println("password is:"+user.getPassword());    return "demo/index";  }

4、使用@ModelAttribute註解擷取POST請求的FORM表單資料  

/**   * 4、使用@ModelAttribute註解擷取POST請求的FORM表單資料   * @param user   * @return   */  @RequestMapping(value="/addUser5",method=RequestMethod.POST)  public String addUser5(@ModelAttribute("user") UserModel user) {    System.out.println("username is:"+user.getUsername());    System.out.println("password is:"+user.getPassword());    return "demo/index";  }

5、用註解@RequestParam綁定請求參數到方法入參 

  當請求參數username不存在時會有異常發生,可以通過設定屬性required=false解決,例如:

@RequestParam(value="username", required=false)  **** 若"Content-Type"="application/x-www-form-urlencoded",post get都可以  **** 若"Content-Type"="application/application/json",只適用get   /**   * 5、用註解@RequestParam綁定請求參數到方法入參   * @param username   * @param password   * @return   */  @RequestMapping(value="/addUser6",method=RequestMethod.GET)  public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {    System.out.println("username is:"+username);    System.out.println("password is:"+password);    return "demo/index";  }

6、用request.getQueryString() 擷取spring MVC get請求的參數,只適用get請求

  @RequestMapping(value="/addUser6",method=RequestMethod.GET)  public String addUser6(HttpServletRequest request) {     System.out.println("username is:"+request.getQueryString());     return "demo/index";   }

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.