1. 程式人生 > >springMVC筆記系列(11)——使用 POJO 物件繫結請求引數值

springMVC筆記系列(11)——使用 POJO 物件繫結請求引數值

Spring MVC 會按請求引數名和 POJO 屬性名進行自動匹配,自動為該物件填充屬性值。支援級聯屬性。如:dept.deptId、dept.address.tel 等

說的通俗點就是,平時我們想將請求頁面的表單資料接收並封裝成特定物件的時候,少不了做的是在某個servlet的對應方法中從request中將各個表單引數取出,型別轉換好,構造一個特定型別的物件,再把表單引數都存進去。

如果表單的專案很多,想想那些線上簡歷、考試報名等表單吧,你發現你的技術活完全成了體力活。springMVC除了能夠按照請求的方法、頭、url及引數進行對映外,還能夠使用 POJO 物件繫結請求引數值。只要你能保證請求頁面的表單輸入項的name與POJO物件類的域名稱相同即可。

另外,springMVC的該功能還支援級聯屬性,也就是支援POJO中的域是另一個POJO物件的情況。相應地,表單的name寫為:pojo1的域名.pojo2的域名

具體還是看例子吧。現在有個Person類:

package com.happyBKs.springmvc.beans;

public class Person {
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public
int getAge() { return age; } public void setAge(int age) { this.age = age; } public Location getLocal() { return local; } public void setLocal(Location local) { this.local = local; } String username; int age; Location local; @Override
public String toString() { return "Person [username=" + username + ", age=" + age + ", local=" + local + "]"; } }

注意到了嗎,其中的一個屬性是Location類的,即local包含級聯屬性。Location類如下:

package com.happyBKs.springmvc.beans;

public class Location {

    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getProvince() {
        return province;
    }
    public void setProvince(String province) {
        this.province = province;
    }
    String city;
    String province;

    @Override
    public String toString() {
        return "Location [city=" + city + ", province=" + province + "]";
    }


}

控制器類如下:

package com.happyBKs.springmvc.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.happyBKs.springmvc.beans.Person;

@RequestMapping("/class")
@Controller
public class POJORequestHandler {

    String page="successrm";

    @RequestMapping("/boy")
    public String handle(Person stud)
    {
        System.out.println(stud.toString());
        return page;
    }
}

請求頁面如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="class/boy" method="post">
username:<input type="text" name="username"/>
<br/>
age:<input type="text" name="age"/>
<br/>
city:<input type="text" name="local.city"/>
<br/>
province:<input type="text" name="local.province"/>
<br/>
<input type="submit" value="GO"/>
</form>
</body>
</html>

執行過程:
這裡寫圖片描述

這裡寫圖片描述

控制檯輸出:

Person [username=happBKs, age=2, local=Location [city=urban, province=Shanghai]]

注意:

如果請求的表單引數中不存在pojo物件中的某個域名稱的專案,則繫結後pojo該屬性為null。

如果請求的表單引數中存在pojo物件中沒有包含的域的專案,則繫結後該引數丟失。

如果表單專案的資料會自動從String轉換為相應pojo的域型別。但如果型別無法轉換,則報錯。

    如我輸入age為dafsdfa,那麼報錯。

這裡寫圖片描述