1、建立一個maven-archetype-webapp項目,pom.xml中添加Maven依賴
<!-- spring mvc相關依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!-- jackson相關依賴 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.3</version> </dependency>
2、在resource文件夾添加一個springmvc-config.xml文件,並設置相關配置
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 包掃描 --> <context:component-scan base-package="com.app.*" /> <!-- 注解驅動 --> <mvc:annotation-driven /> <!-- html文件處理 --> <mvc:resources mapping="/*.html" location="/" /> <!-- 視圖解析 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans>
3、在web.xml里面配置Spring MVC的DispatcherServlet跟編碼過濾器
<?xml version="1.0" encoding="UTF-8"?> <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"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> </servlet> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
到這一步基本的環境配置就可以了,以下的內容為測試環境可不可用使用,如果只是單純配置環境,下面的步驟可以忽略。
4、創建一個實體類User
package com.app.entity; import java.io.Serializable; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonFormat; @SuppressWarnings("serial") public class User implements Serializable { private String userName; @DateTimeFormat(pattern = "yyyy-MM-dd") private Date birth; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+08") public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } }
5、創建一個UserController類,並添加一個測試方法,功能是將添加的數據返回到客戶端顯示
package com.app.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.app.entity.User; @Controller public class UserController { @RequestMapping(value = "/insertUser") @ResponseBody public User insertUser(User user) { return user; } }
6、新建一個html文件,添加如下代碼
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>測試頁面</title> <style> #content { text-align: center; } </style> </head> <body> <div id="content"> <form action="insertUser"> 姓名:<input id="userName" name="userName" type="text"><br> 生日:<input id="birth" name="birth" type="text"><br> <input type="submit" value="測試"> </form> </div> </body> </html>
7、測試與結果
運行項目進入index.html,輸入相關的信息
點擊“測試”按鈕,即可看到結果
-------------------- 額外內容 --------------------
1、@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+08")注解用於返回到客戶端顯示時的格式,里面的timezone不設置,返回時會少一天。
2、給自動生成的web.xml添加filter時會報錯,將最前的以下內容
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
替換成
<?xml version="1.0" encoding="UTF-8"?> <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">
即可。