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

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

 更新時(shí)間:2018年01月11日 11:15:28   作者:平凡的LL  
這篇文章主要介紹了springboot獲取URL請(qǐng)求參數(shù)的多種方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

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

 /**
   * 1.直接把表單的參數(shù)寫(xiě)在Controller相應(yīng)的方法的形參中
   * @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(chēng)一致。

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

  /**
   * 2、通過(guò)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、通過(guò)一個(gè)bean來(lái)接收,post方式和get方式都可以。

(1)建立一個(gè)和表單中參數(shù)對(duì)應(yīng)的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)用這個(gè)bean來(lái)封裝接收的參數(shù)

/**
   * 3、通過(guò)一個(gè)bean來(lái)接收
   * @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、通過(guò)@PathVariable獲取路徑中的參數(shù)

 /**
   * 4、通過(guò)@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";
  }

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

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

Jsp表單如下:

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

Java Controller如下:

  /**
   * 5、使用@ModelAttribute注解獲取POST請(qǐng)求的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綁定請(qǐng)求參數(shù)到方法入?yún)?/p>

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

 /**
   * 6、用注解@RequestParam綁定請(qǐng)求參數(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";
  }

總結(jié)

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

相關(guān)文章

  • 在Java下利用log4j記錄日志的方法

    在Java下利用log4j記錄日志的方法

    本文先對(duì)log4j進(jìn)行了簡(jiǎn)短的介紹,而后通過(guò)安裝、配置和普通項(xiàng)目和web項(xiàng)目幾個(gè)方面來(lái)詳細(xì)介紹了在Java下利用log4j記錄日志的方法,有需要的朋友們可以參考借鑒。
    2016-09-09
  • 從log4j2到Disruptor詳解

    從log4j2到Disruptor詳解

    這篇文章主要介紹了從log4j2到Disruptor詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 詳解在springboot中使用Mybatis Generator的兩種方式

    詳解在springboot中使用Mybatis Generator的兩種方式

    這篇文章主要介紹了詳解在springboot中使用Mybatis Generator的兩種方式,本文將介紹到在springboot的項(xiàng)目中如何去配置和使用MBG以及MBG生成代碼的兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-11-11
  • 關(guān)于logback.xml和logback-spring.xml的區(qū)別及說(shuō)明

    關(guān)于logback.xml和logback-spring.xml的區(qū)別及說(shuō)明

    這篇文章主要介紹了關(guān)于logback.xml和logback-spring.xml的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • java實(shí)現(xiàn)圖片分割指定大小

    java實(shí)現(xiàn)圖片分割指定大小

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)圖片分割指定大小,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Springboot 整合 Java DL4J 實(shí)現(xiàn)文物保護(hù)系統(tǒng)的詳細(xì)過(guò)程

    Springboot 整合 Java DL4J 實(shí)現(xiàn)文物保護(hù)系統(tǒng)的詳細(xì)過(guò)程

    在數(shù)字化時(shí)代,文物保護(hù)尤為關(guān)鍵,本文介紹如何利用SpringBoot和Deeplearning4j構(gòu)建一個(gè)圖像識(shí)別的文物保護(hù)系統(tǒng),系統(tǒng)采用卷積神經(jīng)網(wǎng)絡(luò)(CNN),能夠識(shí)別文物的損壞情況,本文介紹Springboot 整合 Java DL4J 實(shí)現(xiàn)文物保護(hù)系統(tǒng),感興趣的朋友一起看看吧
    2024-10-10
  • org.springframework.beans.BeanInstantiationException異常解決

    org.springframework.beans.BeanInstantiationException異常解決

    本文主要介紹了org.springframework.beans.BeanInstantiationException異常解決,大多數(shù)情況下,這個(gè)異常是由于簡(jiǎn)單的配置錯(cuò)誤或者代碼問(wèn)題導(dǎo)致的,下面就來(lái)具體解決一下
    2024-03-03
  • springboot+mybatis報(bào)錯(cuò)找不到實(shí)體類(lèi)的問(wèn)題

    springboot+mybatis報(bào)錯(cuò)找不到實(shí)體類(lèi)的問(wèn)題

    這篇文章主要介紹了springboot+mybatis報(bào)錯(cuò)找不到實(shí)體類(lèi)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • MybatisPlus的LambdaQueryWrapper用法詳解

    MybatisPlus的LambdaQueryWrapper用法詳解

    LambdaQueryWrapper<Tag>?是 MyBatis-Plus 框架中的一個(gè)功能強(qiáng)大的查詢構(gòu)造器,它用于構(gòu)建 SQL 查詢條件,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-10-10
  • IDEA2019.3配置Hibernate的詳細(xì)教程(未使用IDEA的自動(dòng)化)

    IDEA2019.3配置Hibernate的詳細(xì)教程(未使用IDEA的自動(dòng)化)

    這篇文章主要介紹了IDEA2019.3配置Hibernate的詳細(xì)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05

最新評(píng)論