Spring 如何處理Controller

1、首先說一下 @ModelAttribute  @ModelAttribute 綁定請求參數到命令對象

@ModelAttribute一個具備以下三個做用:html

①綁定請求參數到命令對象:放在功能處理方法的入參上時,用於將多個請求參數綁定到一個命令對象,從而簡化綁java

定流程,並且自動暴露爲模型數據用於視圖頁面展現時使用;spring

②暴露表單引用對象爲模型數據:放在處理器的通常方法(非功能處理方法)上時,是爲表單準備要展現的表單引用session

對象,如註冊時須要選擇的所在城市等,並且在執行功能處理方法(@RequestMapping 註解的方法)以前,自動添加mvc

到模型對象中,用於視圖頁面展現時使用;app

③暴露@RequestMapping 方法返回值爲模型數據:放在功能處理方法的返回值上時,是暴露功能處理方法的返回值爲this

模型數據,用於視圖頁面展現時使用。spa

以下是從spring API上面摘錄的點擊查看官方API文檔
code

1.1 方法外的註釋

Using @ModelAttribute on a methodorm

The @ModelAttribute annotation can be used on methods or on method arguments. This section explains its usage on methods while the next section explains its usage on method arguments.

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. A couple of examples:

// Add one attribute
// The return value of the method is added to the model under the name "account"
// You can customize the name via @ModelAttribute("myAccount")

@ModelAttribute
public Account addAccount(@RequestParam String number) {
    return accountManager.findAccount(number);
}

// Add multiple attributes

@ModelAttribute
public void populateModel(@RequestParam String number, Model model) {
    model.addAttribute(accountManager.findAccount(number));
    // add more ...
}

@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. The latter case is further discussed in the next section.

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. You can choose between the two styles depending on your needs.

A controller can have any number of @ModelAttribute methods. All such methods are invoked before @RequestMapping methods of the same controller.//全部 被 @ModelAttribute 標記的方法都將先執行

@ModelAttribute methods can also be defined in an @ControllerAdvice-annotated class and such methods apply to many controllers. See the the section called 「Advising controllers with the @ControllerAdvice annotation」 section for more details.

若是一個 model attribute 沒有被顯示申明出來。

What happens when a model attribute name is not explicitly specified? In such cases a default name is assigned to the model attribute based on its type. For example if the method returns an object of type Account, the default name used is "account". You can change that through the value of the @ModelAttribute annotation. If adding attributes directly to the Model, use the appropriate overloaded addAttribute(..) method - i.e., with or without an attribute name.

1.2 方法內的註釋

Using @ModelAttribute on a method argument

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.

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) { }

實例化pet 的三種方法:

1.2.1 It may already be in the model due to use of @SessionAttributes 

see the section called 「Using @SessionAttributes to store model attributes in the HTTP session between requests」.

1.2.2 It may already be in the model due to an @ModelAttribute method in the same controller — as explained in the previous section.
1.2.3 It may be retrieved based on a URI template variable and type converter (explained in more detail below).
1.2.4 It may be instantiated using its default constructor.

1.3  URI template variable

@RequestMapping(value="/accounts/{account}", method = RequestMethod.PUT)
public String save(@ModelAttribute("account") Account account) {
}

In this example the name of the model attribute (i.e. "account") matches the name of a URI template variable. If you register Converter<String, Account> that can turn the String account value into an Account instance, then the above example will work without the need for an @ModelAttribute method.

相關文章
相關標籤/搜索