@ModelAttribute 注解可被應(yīng)用在方法或方法參數(shù)上
-
對方法使用@ModelAttribute注解######
注解在方法上的 @ModelAttribute 說明了方法的作用是用于添加一個或多個屬性到model上妥泉。
這樣的方法能接受與 @RequestMapping 注解相同的參數(shù)類型山叮,只不過不能直接被映射到具體的請求上怜械。
在同一個控制器中番宁,注解了 @ModelAttribute 的方法實(shí)際上會在 @RequestMapping 方法之前被調(diào)用
在方法使用@ModelAttribute 方法的兩種風(fēng)格委粉,詳情示例代碼如下:
public class Account implements Serializable {
private static final long serialVersionUID = -3075041060818483817L;
private String name;
private Integer age;
// getter and setter
}
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/modelAtt")
public class ModeAttributeTest {
/**
* 方法-:
* 方法通過返回值的方式默認(rèn)地將添加一個屬性
*
* 屬性名沒有被顯式指定的時:框架將根據(jù)屬性的類型給予一個默認(rèn)名稱
* 例如:本例返回一個 Account 類型的對象野芒,則默認(rèn)的屬性名為"account"
* 你可以通過設(shè)置 @ModelAttribute 注解的值來改變默認(rèn)值 @ModelAttribute("myAccount")
* @param name
* @return
*/
@ModelAttribute
public Account addAccount(@RequestParam(value = "name",defaultValue = "test")String name) {
Account ac = new Account();
ac.setName(name);
ac.setAge(12);
return ac;
}
/**
* 方法二:
* 方法接收一個 Model 對象蓄愁,然后可以向其中添加任意數(shù)量的屬性
* @param number
* @param model
*/
@ModelAttribute
public void populateModel(@RequestParam(value = "number",defaultValue = "123") String number, Model model) {
model.addAttribute("number", number);
model.addAttribute("other", "other");
}
@RequestMapping("/hello")
public ModelAndView hello(){
ModelAndView modelAndView = new ModelAndView("hello");
return modelAndView;
}
}
相關(guān)頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
賬戶名稱:${account.name}<br/>
年齡:${account.age}<br/>
number:${number}<br/>
other:${other}<br/>
</body>
</html>
請求:http://localhost:8085/modelAtt/hello?name=zhangsan&number=123
@ModelAttribute 方法通常被用來填充一些公共需要的屬性或數(shù)據(jù),比如一個下拉列表所預(yù)設(shè)的幾種狀態(tài)狞悲,或者寵物的幾種類型涝登,或者去取得一個HTML表單渲染所需要的命令對象,比如 Account 等
注意
@ModelAttribute 注解也可以被用在 @RequestMapping 方法上效诅。這種情況下胀滚, @RequestMapping 方法的返回值將會被解釋為model的一個屬性,而非一個視圖名乱投。此時視圖名將以視圖命名約定來方式來決議咽笼,與返回值為void的方法所采用的處理方法類似
以我的配置為例
<!-- 視圖解析 -->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
@Controller
@RequestMapping("/modelAtt")
public class ModeAttributeTest {
@ModelAttribute("key")
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
此時在瀏覽器中請求/hello 時,會去找/WEB-INF/jsp/modelAtt/hello.jsp此頁面戚炫,并可在頁面中獲取附帶的數(shù)據(jù)${key}
-
在方法參數(shù)上使用@ModelAttribute注解######
注解在方法參數(shù)上的 @ModelAttribute 說明了該方法參數(shù)的值將由model中取得剑刑。如果model中找不到,那么該參數(shù)會先被實(shí)例化,然后被添加到model中施掏。在model中存在以后钮惠,請求中所有名稱匹配的參數(shù)都會填充到該參數(shù)中。這在Spring MVC中被稱為數(shù)據(jù)綁定七芭,一個非常有用的特性素挽,節(jié)約了你每次都需要手動從表格數(shù)據(jù)中轉(zhuǎn)換這些字段數(shù)據(jù)的時間。
@RequestMapping("/hello")
public ModelAndView hello(@ModelAttribute Account account){
account.setAge(12);
account.setName("456");
return new ModelAndView("hello");
}
上面的代碼只是一種簡單的使用方法
其實(shí)account的來源可由以下幾種方式獲得
- 它可能因?yàn)?@SessionAttributes 注解的使用已經(jīng)存在于model中
- 它可能因?yàn)樵谕瑐€控制器中使用了 @ModelAttribute 方法已經(jīng)存在于model中
- 它可能是由URI模板變量和類型轉(zhuǎn)換中取得的
- 它可能是調(diào)用了自身的默認(rèn)構(gòu)造器被實(shí)例化出來的