spring mvc中的@ModelAttribute注解示例介紹
前言
本文介紹在spring mvc中非常重要的注解@ModelAttribute.這個(gè)注解可以用在方法參數(shù)上,或是方法聲明上。這個(gè)注解的主要作用是綁定request或是form參數(shù)到模型對(duì)象??梢允褂帽4嬖趓equest或session中的對(duì)象來(lái)組裝模型對(duì)象。注意,被@ModelAttribute注解的方法會(huì)在controller方法(@RequestMapping注解的)之前執(zhí)行。因?yàn)槟P蛯?duì)象要先于controller方法之前創(chuàng)建。
請(qǐng)看下面的例子
- ModelAttributeExampleController.java 是controller類(lèi),同時(shí)包含@ModelAttribute 方法。
- UserDetails.java是本例中的模型對(duì)象
- 最后是spring的配置文件
//ModelAttributeExampleController.java
package javabeat.net;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class ModelAttributeExampleController {
@Autowired
private UserDetails userDetails;
@RequestMapping(value="/modelexample")
public String getMethod(@ModelAttribute UserDetails userDetails){
System.out.println("User Name : " + userDetails.getUserName());
System.out.println("Email Id : " + userDetails.getEmailId());
return "example";
}
//This method is invoked before the above method
@ModelAttribute
public UserDetails getAccount(@RequestParam String user, @RequestParam String emailId){
System.out.println("User Value from Request Parameter : " + user);
userDetails.setUserName(user);
userDetails.setEmailId(emailId);
return userDetails;
}
}
//UserDetails.java
package javabeat.net;
public class UserDetails {
private String userName;
private String emailId;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-2.5.xsd"> <context:component-scan base-package="org.spring.examples" /> <bean id="userDetails" class="org.spring.examples.UserDetails"/> </beans>
- 上面的例子,getAccount方法使用@ModelAttribute注解。這意味著方法會(huì)在controller的方法之前執(zhí)行。這個(gè)方法會(huì)使用request的參數(shù)設(shè)置模型對(duì)象。這是一種在方法中設(shè)置值的途徑。
- 另一種@ModelAttribute注解的使用方法,是用在方法的參數(shù)上。在調(diào)用方法的時(shí)候,模型的值會(huì)被注入。這在實(shí)際使用時(shí)非常簡(jiǎn)單。將表單屬性映射到模型對(duì)象時(shí),這個(gè)注解非常有用。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
JAVA時(shí)間存儲(chǔ)類(lèi)Period和Duration使用詳解
這篇文章主要為大家介紹了JAVA時(shí)間存儲(chǔ)類(lèi)Period和Duration使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
JavaWeb中HttpSession中表單的重復(fù)提交示例
這篇文章主要介紹了JavaWeb中HttpSession中表單的重復(fù)提交,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
使用SpringBoot配置https(SSL證書(shū))
這篇文章主要介紹了使用SpringBoot配置https(SSL證書(shū)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot集成kafka全面實(shí)戰(zhàn)記錄
在實(shí)際開(kāi)發(fā)中,我們可能有這樣的需求,應(yīng)用A從TopicA獲取到消息,經(jīng)過(guò)處理后轉(zhuǎn)發(fā)到TopicB,再由應(yīng)用B監(jiān)聽(tīng)處理消息,即一個(gè)應(yīng)用處理完成后將該消息轉(zhuǎn)發(fā)至其他應(yīng)用,完成消息的轉(zhuǎn)發(fā),這篇文章主要介紹了SpringBoot集成kafka全面實(shí)戰(zhàn),需要的朋友可以參考下2021-11-11

