国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > Java > 正文

springboot獲取URL請求參數(shù)的多種方式

2024-07-13 10:16:25
字體:
來源:轉載
供稿:網(wǎng)友

 1、直接把表單的參數(shù)寫在Controller相應的方法的形參中,適用于get方式提交,不適用于post方式提交。

 /**   * 1.直接把表單的參數(shù)寫在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";  }

url形式:http://localhost/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 提交的參數(shù)需要和Controller方法中的入?yún)⒚Q一致。

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方式都可以。

(1)建立一個和表單中參數(shù)對應的bean

package demo.model;public class UserModel {  private String username;  private String password;  public String getUsername() {    return username;  }  public void setUsername(String username) {    this.username = username;  }  public String getPassword() {    return password;  }  public void setPassword(String password) {    this.password = password;  }}

(2)用這個bean來封裝接收的參數(shù)

/**   * 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、通過@PathVariable獲取路徑中的參數(shù)

 /**   * 4、通過@PathVariable獲取路徑中的參數(shù)   * @param username   * @param password   * @return   */  @RequestMapping(value="/addUser4/{username}/{password}",method=RequestMethod.GET)public String addUser4(@PathVariable String username,@PathVariable String password) {    System.out.println("username is:"+username);    System.out.println("password is:"+password);    return "demo/index";  }

例如,訪問http://localhost/SSMDemo/demo/addUser4/lixiaoxi/111111 路徑時,則自動將URL中模板變量{username}和{password}綁定到通過@PathVariable注解的同名參數(shù)上,即入?yún)⒑髐sername=lixiaoxi、password=111111。

5、使用@ModelAttribute注解獲取POST請求的FORM表單數(shù)據(jù)

Jsp表單如下:

<form action ="<%=request.getContextPath()%>/demo/addUser5" method="post">    用戶名: <input type="text" name="username"/><br/>   密  碼: <input type="password" name="password"/><br/>   <input type="submit" value="提交"/>    <input type="reset" value="重置"/> </form>

Java Controller如下:

  /**   * 5、使用@ModelAttribute注解獲取POST請求的FORM表單數(shù)據(jù)   * @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";  }

6、用注解@RequestParam綁定請求參數(shù)到方法入?yún)?/p>

當請求參數(shù)username不存在時會有異常發(fā)生,可以通過設置屬性required=false解決,例如: @RequestParam(value="username", required=false)

 /**   * 6、用注解@RequestParam綁定請求參數(shù)到方法入?yún)?  * @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";  }

總結

以上所述是小編給大家介紹的springboot獲取URL請求參數(shù)的多種方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網(wǎng)網(wǎng)站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 洮南市| 墨竹工卡县| 敦化市| 清水河县| 麻江县| 津市市| 乌海市| 新竹市| 浪卡子县| 道真| 嘉峪关市| 广丰县| 广水市| 辉县市| 南郑县| 宁德市| 志丹县| 太湖县| 开原市| 米泉市| 桂阳县| 通海县| 晋州市| 盐城市| 桐柏县| 北流市| 武定县| 黑河市| 上蔡县| 阿荣旗| 和田市| 石狮市| 阳城县| 黄平县| 涞源县| 塔河县| 瓦房店市| 日土县| 金坛市| 西畴县| 常山县|