预期的效果:填写一个表单,提交给Controller处理,返回结果界面。
效果的展示:表单的样式
结果样式:
程序的思路:填写一个表单,产生一个action,根据Action找到对应的@RequestMapping进行处理,我们用@ModelAttribute将传递过来的表单封装到User类中,然后将类传到model中,返回到结果页面,结果页面通过@ModelAttribute得到类数据,进行显示输出。
下面给出源代码:
类User:我们对类进行了格式化管理。
表单界面源代码:
<%-- Created by IntelliJ IDEA. User: ly Date: 2018/3/13 Time: 19:37 To change this template use File | Settings | File Templates. --%> <%@ 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>测试AnnotationFormatterFactory 接口</title> </head> <body> <h3>测试表单数据格式化</h3> <form action="getForm" method="post"> <table> <tr> <td><label>日期类型: </label></td> <td><input type="text" id="birthday" name="birthday"></td> </tr> <tr> <td><label>整数类型: </label></td> <td><input type="text" id="total" name="total"></td> </tr> <tr> <td><label>百分数类型: </label></td> <td><input type="text" id="discount" name="discount"></td> </tr> <tr> <td><input id="submit" type="submit" value="提交"></td> </tr> </table> </form> </body> </html>
Controller源代码:
package org.spring.controller; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.spring.domain.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class FormatterController { private static final Log logger = LogFactory.getLog(FormatterController.class); @RequestMapping(value = "/{formName}") public String loginForm(@PathVariable String formName) { // 动态跳转页面 return formName; } @RequestMapping(value = "/getForm", method = RequestMethod.POST) public String test( @ModelAttribute User user, Model model) { logger.info(user); model.addAttribute("user", user); return "success"; } }
<%-- Created by IntelliJ IDEA. User: ly Date: 2018/3/13 Time: 19:37 To change this template use File | Settings | File Templates. --%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> <!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>测试AnnotationFormatterFactory</title> </head> <body> <h3>测试表单数据格式化</h3> <%-- modelAttribute 获取绑定的model属性 --%> <form:form modelAttribute="user" method="post" action=""> <table> <tr> <td>日期类型:</td> <td><form:input path="birthday"/></td> </tr> <tr> <td>整数类型:</td> <td><form:input path="total"/></td> </tr> <tr> <td>百分数类型:</td> <td><form:input path="discount"/></td> </tr> </table> </form:form> </body> </html>
附:Maven的配置以及web.xml配置
web.xml:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <!-- <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>--> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <!-- 定义Spring MVC的前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 让Spring MVC的前端控制器拦截所有请求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--过滤设置--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> </web-app>
pox.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>cn.nk</groupId> <artifactId>sping</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>sping Maven Webapp</name> <url>http://maven.apache.org</url> <!-- spring版本号 --> <properties> <spring.version>4.2.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 添加spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.5.RELEASE</version> </dependency> <!-- 映入JSON --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.1.26</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.37</version> </dependency> </dependencies> <build> <finalName>sping</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>