SpringMVC學習(三)對映請求引數&請求引數

GensingU發表於2017-07-24

1.使用@RequestParam來繫結請求引數,程式碼如下:

<a href="TestRequestMapping/TestRequestParam?username=username&age=10">TestRequestParam</a>
/**
     * 使用@RequestParam繫結請求引數
     * @param username
     * @param age
     * @return
     * value 表示請求引數的名稱
     * required 表示該引數是否必須(true或者false)
     * defaultValue 表示該引數的預設值
     */
    @RequestMapping("TestRequestParam")
public String testRequestParam(@RequestParam(value="username",required=false,defaultValue="zhangsan") String username,
@RequestParam(value="age") Integer age)
    {
        System.out.println("username:"+username+"  "+"age=:"+age);
        return "success";
    }

其中:
value 表示請求引數的名稱
required 表示該引數是否必須(true或者false)
defaultValue 表示該引數的預設值

此外,還可以用@RequestHeader來對映請求頭以及@CookieValue來對映Cookie值,用法與@RequestParam相同

2.使用POJO物件繫結請求引數
SpringMVC會按照請求引數名和POJO屬性名進行自動的匹配,自動為該物件填充屬性,例如User.address、User.name等,且支援級聯屬性
程式碼如下:
首先先寫兩個POJO類

/**
 * User實體類
 * @author YZX
 *
 */
public class User {
    private String userName;
    private String password;
    private String email;
    private int age;
    private Address address;
    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 String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "User [userName=" + userName + ", password=" + password
                + ", email=" + email + ", age=" + age + ", address=" + address
                + "]";
    }

}
/**
 * Address實體類
 * @author YZX
 *
 */
public class Address {
    private String province;
    private String city;
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    @Override
    public String toString() {
        return "Address [province=" + province + ", city=" + city + "]";
    }


}

接著寫一個表單用於傳送請求

<form action="TestRequestMapping/TestPojo" method="post">
        username:<input type="text" name="userName">
        <br>
        password:<input type="password" name="password">
        <br>
        email:<input type="text" name="email">
        <br>
        age:<input type="text" name="age">
        <br>
        province:<input type="text" name="address.procince">
        <br>
        city:<input type="text" name="address.city">
        <br>
        <input type="submit" value="Submit">
    </form>

編寫一個方法用於響應請求

@RequestMapping("TestPojo")
public String testPojo(User user)
{
    System.out.println(user);
    return "success";
}

從上述可以看出,該方法傳入了一個User類作為引數,而表單中的userName,password,email,age均為User的屬性,而provider以及city則為User類的address屬性的屬性。但要注意,請求引數名必須與POJO的屬性名相同才能自動匹配,否則會出現錯誤。

3.使用Servlet原生的API作為請求引數
可以使用Servlet原生API作為目標方法的引數:
①HttpServletReque
②HttpServletResponse
③HttpSession
④java.security.principal
⑤Reader
⑥Writer

相關文章