Spring MVC @Modelattribute示例
@modelattribute是最重要的Spring MVC注释之一。
@ModelAttribute用于将请求参数与Model对象绑定。
让我们说你有10个请求参数,可以将它们包装到模型对象并将其传递给handler方法。
有两种方法可以创建模型对象。
- 对象已存在于会话或者ModelMap对象中。
- 使用@ModelAttribute注释注释另一种方法并创建模型对象。在实际处理程序调用之前将调用此方法。
例子:
以下是创建Spring MVC ModelAttribute示例的步骤。
1)使用名为"springmvcmodelattributexample"的Eclipse中的Maven创建动态Web项目
Maven依赖项
2)我们需要在类路径中添加Hymanson Json实用程序。
<dependency> <groupId>com.fasterxml.Hymanson.core</groupId> <artifactId>Hymanson-databind</artifactId> <version>2.4.1</version> </dependency>
Spring将Hymanson2jsonmessageConverter自动加载到其应用程序上下文中。
每当我们使用Accep Headers ="Accept = Application/JSON"时请求资源作为JSON,那么Hymanson2jsonMessageConverter就会进入图片并将资源转换为JSON格式。
现在更改pom.xml如下:pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.igi.theitroad</groupId> <artifactId>SpringRestfulWebServicesWithJSONExample</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringRestfulWebServicesWithJSONExample Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>com.fasterxml.Hymanson.core</groupId> <artifactId>Hymanson-databind</artifactId> <version>2.4.1</version> </dependency> </dependencies> <build> <finalName>SpringRestfulWebServicesWithJSONExample</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> <properties> <spring.version>4.2.1.RELEASE</spring.version> <jdk.version>1.7</jdk.version> </properties> </project>
Spring应用程序配置:
3)更改Web.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springrest</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springrest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
4)在/Web-Inf /文件夹中创建名为springrest-servlet.xml的XML文件。
请更改上下文:组件扫描如果要使用不同的套装来搜索控制器。
请参阅Spring MVC Hello World示例以获取更多的理解。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:annotation-driven <context:component-scan base-package="org.igi.theitroad.controller" <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <mvc:default-servlet-handler </beans>
创建bean类
5)在org.igi.theitroad.Bean中创建bean名称"country.java"。
package org.igi.theitroad.bean; public class Country{ String countryName; long population; public Country() { super(); } public Country(String countryName,long population) { super(); this.countryName = countryName; this.population=population; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public long getPopulation() { return population; } public void setPopulation(long population) { this.population = population; } }
创建控制器
6)在Package org.igi.theitroad.Controller中创建名为"CountryController.java"的控制器
package org.igi.theitroad.controller; import org.igi.theitroad.bean.Country; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @Controller public class CountryController { @ModelAttribute public Country getCountry(@RequestParam String countryName, @RequestParam long population) { Country country=new Country(); country.setCountryName(countryName); country.setPopulation(population); return country; } @RequestMapping(value = "/addCountry", method = RequestMethod.POST) public String addCountry(@ModelAttribute Country country,ModelMap model) { model.addAttribute("countryName", country.getCountryName()); model.addAttribute("population", country.getPopulation()); return "countryDetails"; } }
Create view
修改下面的index.jsp a s
<head> <style> .blue-button{ background: #25A6E1; filter: progid: DXImageTransform.Microsoft.gradient( startColorstr='#25A6E1',endColorstr='#188BC0',GradientType=0); padding:3px 5px; color:#fff; font-family:'Helvetica Neue',sans-serif; font-size:12px; border-radius:2px; -moz-border-radius:2px; -webkit-border-radius:4px; border:1px solid #1A87B9 } table { font-family: "Helvetica Neue", Helvetica, sans-serif; width: 50%; } th { background: SteelBlue; color: white; } td,th{ border: 1px solid gray; width: 25%; text-align: left; padding: 5px 10px; } </style> </head> <body> <form action="addCountry" method="post"> <table> <tr> <th colspan="2">Add Country</th> </tr> <tr> <td>Country</td> <td><input type="text" name=countryName></td> </tr> <tr> <td>Population</td> <td><input type="text" name=population> </td></tr> <tr> <td colspan="2"><input type="submit" class="blue-button" </td> </tr> </table> </form> </body>
在/web-inf /文件夹中创建countrydetails.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Country Details</title> </head> <body> Added country : <h3>Country Name: {countryName}</h3> <h3>Population : ${population}</h3> </body> </html>
7)这是时候做Maven建造了。
右键单击项目 - >运行AS - > Maven Build
8)将目标作为清洁安装(下面给出),然后单击运行
运行应用程序
9)右键单击项目 - >在服务器上运行AS - >运行
选择Apache Tomcat,然后单击"完成"
10)单击"提交"按钮时,我们将看到以下屏幕。