最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring mvc中的@ModelAttribute注解示例介紹

 更新時(shí)間:2017年09月06日 10:13:20   作者:沈子平  
在Spring mvc中,注解@ModelAttribute是一個(gè)非常常用的注解,下面這篇文章主要給大家介紹了關(guān)于spring mvc中@ModelAttribute注解的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。

前言

本文介紹在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類,同時(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ǔ)類Period和Duration使用詳解

    JAVA時(shí)間存儲(chǔ)類Period和Duration使用詳解

    這篇文章主要為大家介紹了JAVA時(shí)間存儲(chǔ)類Period和Duration使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Maven resrouce下filtering的使用方法

    Maven resrouce下filtering的使用方法

    本文介紹了Maven的resource插件中的filtering功能,該功能用于在構(gòu)建過(guò)程中將資源目錄下的文件中的tokens進(jìn)行參數(shù)替換,tokens的來(lái)源可以是pom文件中的properties屬性或外部的.properties文件,通過(guò)這種方式,可以靈活地切換不同開發(fā)環(huán)境下的配置屬性
    2024-11-11
  • 快速了解Spring Boot

    快速了解Spring Boot

    這篇文章主要介紹了快速了解Spring Boot,介紹了其環(huán)境準(zhǔn)備,URL中的變量以及模板渲染等內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • JavaWeb中HttpSession中表單的重復(fù)提交示例

    JavaWeb中HttpSession中表單的重復(fù)提交示例

    這篇文章主要介紹了JavaWeb中HttpSession中表單的重復(fù)提交,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • Java迭代器與Collection接口超詳細(xì)講解

    Java迭代器與Collection接口超詳細(xì)講解

    Collection也稱集合,集合概述:集合是Java中提供的一種容器,可以用來(lái)存儲(chǔ)多個(gè)數(shù)據(jù)。Iterator(迭代器)不是一個(gè)集合,它是一種用于訪問(wèn)集合的方法,可用于迭代 ArrayList 和 HashSet 等集合
    2022-07-07
  • 使用SpringBoot配置https(SSL證書)

    使用SpringBoot配置https(SSL證書)

    這篇文章主要介紹了使用SpringBoot配置https(SSL證書),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 基于java讀取并引用自定義配置文件

    基于java讀取并引用自定義配置文件

    這篇文章主要介紹了基于java讀取并引用自定義配置文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 深入淺析TomCat Session管理分析

    深入淺析TomCat Session管理分析

    這篇文章主要介紹了深入淺析TomCat Session管理分析,需要的朋友可以參考下
    2015-11-11
  • SpringBoot集成kafka全面實(shí)戰(zhàn)記錄

    SpringBoot集成kafka全面實(shí)戰(zhàn)記錄

    在實(shí)際開發(fā)中,我們可能有這樣的需求,應(yīng)用A從TopicA獲取到消息,經(jīng)過(guò)處理后轉(zhuǎn)發(fā)到TopicB,再由應(yīng)用B監(jiān)聽處理消息,即一個(gè)應(yīng)用處理完成后將該消息轉(zhuǎn)發(fā)至其他應(yīng)用,完成消息的轉(zhuǎn)發(fā),這篇文章主要介紹了SpringBoot集成kafka全面實(shí)戰(zhàn),需要的朋友可以參考下
    2021-11-11
  • 深入理解spring boot 監(jiān)控

    深入理解spring boot 監(jiān)控

    今天通過(guò)本文給大家介紹關(guān)于spring boot 監(jiān)控的相關(guān)知識(shí),引入jar包的實(shí)例代碼文中也給大家詳細(xì)介紹,對(duì)spring boot 監(jiān)控相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-10-10

最新評(píng)論

宜君县| 宜春市| 陵川县| 澳门| 安国市| 新兴县| 东丽区| 托克逊县| 永昌县| 朔州市| 监利县| 新化县| 林州市| 永清县| 江门市| 澳门| 清水县| 太康县| 奉贤区| 沙雅县| 信阳市| 定边县| 灵山县| 通州区| 徐汇区| 东莞市| 麦盖提县| 理塘县| 遵化市| 建昌县| 南丰县| 济阳县| 华阴市| 孟村| 长治市| 安徽省| 溧水县| 岳普湖县| 保定市| 当涂县| 肇州县|