spring mvc中的@ModelAttribute注解示例介紹
前言
本文介紹在spring mvc中非常重要的注解@ModelAttribute.這個(gè)注解可以用在方法參數(shù)上,或是方法聲明上。這個(gè)注解的主要作用是綁定request或是form參數(shù)到模型對(duì)象??梢允褂帽4嬖趓equest或session中的對(duì)象來組裝模型對(duì)象。注意,被@ModelAttribute注解的方法會(huì)在controller方法(@RequestMapping注解的)之前執(zhí)行。因?yàn)槟P蛯?duì)象要先于controller方法之前創(chuàng)建。
請(qǐng)看下面的例子
- ModelAttributeExampleController.java 是controller類,同時(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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
如何用Java來進(jìn)行文件切割和簡(jiǎn)單的內(nèi)容過濾的實(shí)現(xiàn)
這篇文章主要介紹了如何用Java來進(jìn)行文件切割和簡(jiǎn)單的內(nèi)容過濾的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01Java基于Session登錄驗(yàn)證的實(shí)現(xiàn)示例
基于Session的登錄驗(yàn)證方式是最簡(jiǎn)單的一種登錄校驗(yàn)方式,本文主要介紹了Java基于Session登錄驗(yàn)證的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02SpringBoot使用@RestController處理GET和POST請(qǐng)求的代碼詳解
在Spring?MVC中,@RestController注解的控制器類可以處理多種HTTP請(qǐng)求方法,包括GET和POST,所以本文就給大家詳細(xì)介紹了SpringBoot使用@RestController處理GET和POST請(qǐng)求的示例代碼,需要的朋友可以參考下2024-07-07Java實(shí)現(xiàn)FutureTask的示例詳解
在并發(fā)編程當(dāng)中我們最常見的需求就是啟動(dòng)一個(gè)線程執(zhí)行一個(gè)函數(shù)去完成我們的需求,而在這種需求當(dāng)中,我們需要函數(shù)有返回值。Java給我們提供了這種機(jī)制,去實(shí)現(xiàn)這一個(gè)效果:FutureTask。本文為大家準(zhǔn)備了Java實(shí)現(xiàn)FutureTask的示例代碼,需要的可以參考一下2022-08-08如何使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署
這篇文章主要介紹了使用Idea中的 Deployment 實(shí)現(xiàn)打包自動(dòng)部署,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08