[@Controller]4 詳解@ModelAttribute

[@Controller]4 詳解@ModelAttribute

 (2012-06-14 15:44:55)
A@ModelAttribute

Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view. Supported for RequestMapping annotated handler classes.

在被@RequestMapping註釋的處理器類中,這個註釋可以綁定一個方法參數或綁定一個方法的返回值到一個命名的模型屬性,提供給一個視圖。

Can be used to expose command objects to a web view, using specific attribute names, through annotating corresponding parameters of a RequestMapping annotated handler method).

可以用於把一個command對象提供給web視圖,使用指定的屬性名稱,在被@RequestMapping註釋的處理器方法中註釋相關參數。

Can also be used to expose reference data to a web view through annotating accessor methods ina controller class which is based on RequestMapping annotated handler methods, with such accessor methods allowed to have any arguments that RequestMapping supports for handler methods, returning the model attribute value to expose.

可以用於提供數據給一個web視圖,通過註釋處理器方法,這個方法允許有任何參數,返回的模型屬性值被提供。

A.1@ ModelAttribute的屬性

value

The name of the model attribute to bind to.

綁定的模型屬性的名稱。

The default model attribute name is inferred from the declared attribute type (i.e. the method parameter type or method return type), based on the non-qualified class name: e.g. "orderAddress" for class "mypackage.OrderAddress", or "orderAddressList" for "List<mypackage.OrderAddress>".

默認的模型屬性名稱自動判斷聲明的屬性類型(如,方法參數類型或方法返回類型)。如這個值是orderAddress,就對於當前包. OrderAddress

 

B@ModelAttribute註釋一個方法

An @ModelAttribute on a method indicates the purpose of that method is to add one or more model attributes. Such methods support the same argument types as @RequestMapping methods but cannot be mapped directly to requests. Instead @ModelAttribute methods in a controller are invoked before @RequestMapping methods, within the same controller.

@ModelAttribute註釋的方法表示這個方法的目的是增加一個或多個模型(model)屬性。這個方法和被@RequestMapping註釋的方法一樣也支持@RequestParam參數,但是它不能直接被請求映射。實際上,控制器中的@ModelAttribute方法是在同一控制器中的@RequestMapping方法被調用之前調用的。

@ModelAttribute methods are used to populate the model with commonly needed attributes for example to fill a drop-down with states or with pet types, or to retrieve a command object like Account in order to use it to represent the data on an HTML form.

@ModelAttribute註釋的方法用於填充model屬性,例如,爲下拉菜單填充內容,或檢索一個command對象(如,Account),用它來表示一個HTML表單中的數據。

A controller can have any number of @ModelAttribute methods. All such methods are invoked before @RequestMapping methods of the same controller.

一個控制器可以有任意數量的@ModelAttribute方法。所有這些方法都在@RequestMapping方法被調用之前調用。

Note the two styles of @ModelAttribute methods. In the first, the method adds an attribute implicitly by returning it. In the second, the method accepts a Model and adds any number of model attributes to it.

有兩種類型的@ModelAttribute方法。一種是:加入只一個屬性,用方法的返回類型隱含表示。另一種是:方法接受一個Model類型的參數,這個model可以加入任意多個model屬性。

B.1@ModelAttribute註釋void返回值的方法

舉例說明

@Controller

public class HelloWorldController {

    @ModelAttribute

    public void populateModel(@RequestParam String abc, Model model) {

       model.addAttribute("attributeName"abc);

    }

    @RequestMapping(value = "/helloWorld")

    public String helloWorld() {

       return "helloWorld";

    }

}

這個例子,在獲得請求/helloWorld 後,populateModel方法在helloWorld方法之前先被調用,它把請求參數(/helloWorld?abc=text)加入到一個名爲attributeNamemodel屬性中,在它執行後helloWorld被調用,返回視圖名helloWorldmodel已由@ModelAttribute方法生產好了。

這個例子中model屬性名稱和model屬性對象有model.addAttribute()實現,不過前提是要在方法中加入一個Model類型的參數。

B.2@ModelAttribute註釋返回具體類的方法

舉例說明

@ModelAttribute

    public Account addAccount(@RequestParam String number) {

       return accountManager.findAccount(number);

    }

這種情況,model屬性的名稱沒有指定,它由返回類型隱含表示,如這個方法返回Account類型,那麼這個model屬性的名稱是account

這個例子中model屬性名稱有返回對象類型隱含表示,model屬性對象就是方法的返回值。它無須要特定的參數。

B.3@ModelAttribute(value="")註釋返回具體類的方法

舉例說明

@Controller

public class HelloWorldController {

    @ModelAttribute("attributeName")

    public String addAccount(@RequestParam String abc) {

       return abc;

    }

    @RequestMapping(value = "/helloWorld")

    public String helloWorld() {

       return "helloWorld";

    }

}

這個例子中使用@ModelAttribute註釋的value屬性,來指定model屬性的名稱。model屬性對象就是方法的返回值。它無須要特定的參數。

B.4@ModelAttribute@RequestMapping同時註釋一個方法

舉例說明

@Controller

public class HelloWorldController {

    @RequestMapping(value = "/helloWorld.do")

    @ModelAttribute("attributeName")

    public String helloWorld() {

       return "hi";

    }

}

這時這個方法的返回值並不是表示一個視圖名稱,而是model屬性的值,視圖名稱由RequestToViewNameTranslator根據請求"/helloWorld.do"轉換爲helloWorldModel屬性名稱有@ModelAttribute(value=””)指定。

 

C@ModelAttribute註釋一個方法的參數

An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. If not present in the model, the argument should be instantiated first and then added to the model.Once present in the model, the argument's fields should be populated from all request parameters that have matching names. This is known as data binding in Spring MVC, a very useful mechanism that saves you from having to parse each form field individually.

@ModelAttribute註釋方法的一個參數表示應從模型model中取得。若在model中未找到,那麼這個參數將先被實例化後加入到model中。若在model中找到,則請求參數名稱和model屬性字段若相匹配就會自動填充。這個機制對於表單提交數據綁定到對象屬性上很有效。

B.1、從model中獲取

It may already be in the model due to an @ModelAttribute method in the same controller

參數的值從當前控制器的@ModelAttribute方法提供的model屬性中獲取。

舉例說明

@Controller

public class HelloWorldController {

    @ModelAttribute("user")

    public User addAccount() {

       return new User("jz","123");

    }

    @RequestMapping(value = "/helloWorld")

public String helloWorld(@ModelAttribute("user") User user) {

       user.setUserName("jizhou");

       return "helloWorld";

    }

}

在這個例子裏,@ModelAttribute("user") User user註釋方法參數,參數user的值來源於addAccount()方法中的model屬性。

B.2、從URI template變量中獲取

 

B.3、從Form表單或URL參數中獲取

舉例說明

@Controller

public class HelloWorldController {

    @RequestMapping(value = "/helloWorld")

    public String helloWorld(@ModelAttribute User user) {

       return "helloWorld";

    }

}

注意這時候這個User類一定要有沒有參數的構造函數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章