首頁 > 軟體

SpringMVC獲取請求引數筆記整理

2022-04-11 10:01:18

前言

本篇文章目的是為了學習、記錄和分享博主在學習 Spring MVC過程中的筆記。同時也希望本篇文章能夠幫助螢幕前的你!

一、使用ServletAPI獲取引數

通過 HttpServletRequest 當作形參,此時 HttpServletRequest 型別的參數列示封裝了當前請求的請求報文的物件。

測試案例:

 @RequestMapping("/testParam")
    public String testParam(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username+",password:"+password);
        return "success";
    }
<form method="get" action="/SpringMVC/testParam">
    username: <input  type="text"   name="username"/>
    password: <input  type="password"   name="password"/>
    <input type="submit" value="Submit" />
</form>

執行結果:

二、通過控制器方法的形參獲取請求引數

在控制器方法的形參位置,設定和請求引數同名的形參,當瀏覽器傳送請求,匹配到請求對映時,在 DispatcherServlet 中就會將請求引數賦值給相應的形參。

測試用例:

    @RequestMapping("/testParam")
    public String testParam(String username,String password){
        System.out.println("username:"+username+",password:"+password);
        return "success";
    }
<form method="get" action="/SpringMVC/testParam">
    username: <input  type="text"   name="username"/>
    password: <input  type="password"   name="password"/>
    <input type="submit" value="Submit" />
</form>

執行結果:

三、@RequestParam

@RequestParam是將請求引數控制器方法的形參建立對映關係!

OK,我們可以來看一下 @RequestParam 的原始碼部分:

@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
    //別名name,一般預設為空
    @AliasFor("name")
    String value() default "";
	//指定為形參賦值的請求引數的引數名
    @AliasFor("value")
    String name() default "";
	//設定是否必須傳輸此請求引數,預設值為true
    boolean required() default true;
	//用來定義預設值
    String defaultValue() default "nttnttnue000ue001ue002nttttn";
}

注:

required設定為true時,則當前請求必須傳輸value所指定的請求引數,若沒有傳輸該請求引數,且沒有設定defaultValue屬性,則頁面報錯 400:Required String parameter ‘xxx’ is not present; 若設定為false,則當前請求不是必須傳輸value所指定的請求引數,若沒有傳輸,則註解所標識的形參的值為null

    @RequestMapping(value = "/testParam", method = RequestMethod.POST)
    public String testParam(@RequestParam(value = "user_name",required = false,defaultValue = "gg") String name,
                            @RequestParam(value = "password",required = true,defaultValue = "hh") String pwd,
                            String[] hobby) {

        System.out.println(name + pwd + Arrays.toString(hobby));
        return "success";
    }
<form th:action="@{/testParam}" method="post">
    使用者名稱:<input type="name" name = "user_name"><br>
    密碼:<input type="password" name = "password"><br>
    愛好:<input type="checkbox" name = "hobby" value="a">a
    <input type="checkbox" name = "hobby" value="b">b
    <input type="checkbox" name = "hobby" value="c">c<br>
    <input type="submit" value="測試">
</form>

執行結果:

四、@RequestHeader

@RequestHeader是將請求頭資訊和控制器方法的形參建立對映關係。

@RequestHeader註解一共有三個屬性:value、required、defaultValue,用法同@RequestParam

@RequestMapping(value = "/testParam2",method = RequestMethod.POST)
//形參位置的request表示當前請求
public String testParam2(@RequestParam("username") String name,
                         @RequestParam("password") String pwd,
                         String[] hobby,
                         @RequestHeader("Host") String host){
    System.out.println(name+pwd+ Arrays.toString(hobby));
    System.out.println("Host:"+host);//輸出結果Host:localhost:8080
    return "success";
}

五、@CookieValue

@CookieValue是將cookie資料和控制器方法的形參建立對映關係

@CookieValue註解一共有三個屬性:value、required、defaultValue,用法同@RequestParam

@CookieValue("JSESSIONID") String JSESSIONID
@Controller
@RequestMapping("hello")
public class HelloController2 {
    @RequestMapping("show25")
    public String test25(Model model, @CookieValue("JSESSIONID")String jsessionid){
        model.addAttribute("msg", "獲取cookie,jsessionid:" + jsessionid);
        return "hello2";
    }
}

六、通過實體類的形參獲取引數

首先需要建立一個實體類 User

package xiaobao.mvc.bean;
public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private String sex;
    private String email;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    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;
    public Integer getAge() {
        return age;
    public void setAge(Integer age) {
        this.age = age;
    public String getSex() {
        return sex;
    public void setSex(String sex) {
        this.sex = sex;
    public String getEmail() {
        return email;
    public void setEmail(String email) {
        this.email = email;
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                ", age=" + age +
                ", sex='" + sex + ''' +
                ", email='" + email + ''' +
                '}';
}

然後前端:

<form th:action="@{/testpojo}" method="post">
    使用者名稱:<input type="text" name="username"><br>
    密碼:<input type="password" name="password"><br>
    性別:<input type="radio" name="sex" value="男">男<input type="radio" name="sex" value="女">女<br>
    年齡:<input type="text" name="age"><br>
    郵箱:<input type="text" name="email"><br>
    <input type="submit">
</form>

後端:

@RequestMapping("/testpojo")
public String testPOJO(User user){
    System.out.println(user);
    return "success";
}

執行結果:

因為我還沒沒有解決亂碼,所以這個就一直亂碼問題,等到後面我專門出一期這個文章來解決亂碼的問題!

到此這篇關於SpringMVC獲取請求引數筆記整理的文章就介紹到這了,更多相關SpringMVC獲取請求引數內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com