SpringMVC處置Form表單實例。本站提示廣大學習愛好者:(SpringMVC處置Form表單實例)文章只能為提供參考,不一定能成為您想要的結果。以下是SpringMVC處置Form表單實例正文
Spring MVC 表單處置例子上面的例子解釋了若何編寫一個簡略的基於 web 的運用法式,它應用了應用 Spring 的 Web MVC 框架的 HTML 表單。
一 測試項目搭建
(1)新建Java Web項目,並引入幾個SpringMVC項目所須要的jar包,項目構造和所須要的jar包以下:
①web.xml:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.html</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>
這裡界說了SpringMVC攔阻以.html開頭的url後綴並停止處置
②springmvc-servlet.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <context:component-scan base-package="cn.zifangsky.* *.controller" /> <context:annotation-config /> <!-- 激活Bean中界說的注解 --> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
在下面的設置裝備擺設文件中,<context:annotation-config />激活了Bean中界說的一些注解,而<mvc:annotation-driven />則啟動了SpringMVC的一些默許設置裝備擺設。在設置裝備擺設文件的最初則界說了邏輯視圖到現實視圖之間的對應關系,一句話說明就是:給前往的邏輯視圖加上下面界說的途徑前綴和後綴就是現實視圖的真正途徑了。
二 應用SpringMVC處置Form表單
(1)在正式開端之前,先樹立一個model和列舉類:
①實體類User:
package cn.zifangsky.model; import java.time.LocalDate; import org.springframework.format.annotation.DateTimeFormat; public class User { private String name; private String password; private String job; @DateTimeFormat(pattern="yyyy-MM-dd") private LocalDate birthDate; private Gender gender; private String country; private boolean smoking; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public LocalDate getBirthDate() { return birthDate; } public void setBirthDate(LocalDate birthDate) { this.birthDate = birthDate; } public Gender getGender() { return gender; } public void setGender(Gender gender) { this.gender = gender; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public boolean isSmoking() { return smoking; } public void setSmoking(boolean smoking) { this.smoking = smoking; } }
②表現“性別”的列舉類Gender:
package cn.zifangsky.model; public enum Gender { MALE, FEMALE; }
上面將按照法式的履行流程來簡略解釋SpringMVC的Form表單處置,分離是前台的form表單填寫 –>controller處置 –>處置成果視圖頁面
(2)測試項目標首頁與form表單頁面:
①首頁index.jsp:
<% response.sendRedirect("form.html"); %>
可以看出,在這裡我們的首頁很簡略,就是重定向到“form.html”,然則經由過程我們後面在web.xml中的設置裝備擺設,SpringMVC將會對這個要求轉到一個詳細的controller中停止處置,固然這裡就是直接轉到form表單頁面。詳細的controller裡的處置邏輯上面再說
②form表單頁面userForm.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Spring MVC Form Handling</title> </head> <body> <h2>用戶注冊</h2> <mvc:form modelAttribute="user" action="result.html"> <table> <tr> <td><mvc:label path="name">姓名:</mvc:label></td> <td><mvc:input path="name" /></td> </tr> <tr> <td><mvc:label path="password">暗碼:</mvc:label></td> <td><mvc:password path="password" /></td> </tr> <tr> <td><mvc:label path="job">任務:</mvc:label></td> <td><mvc:textarea path="job" /></td> </tr> <tr> <td><mvc:label path="birthDate">誕辰:</mvc:label></td> <td><mvc:input path="birthDate" /></td> </tr> <tr> <td><mvc:label path="gender">性別:</mvc:label></td> <td><mvc:radiobuttons path="gender" items="${genders}" /></td> </tr> <tr> <td><mvc:label path="country">棲身地:</mvc:label></td> <td><mvc:select path="country" items="${countries}" /></td> </tr> <tr> <td><mvc:label path="smoking">抽煙嗎:</mvc:label></td> <td><mvc:checkbox path="smoking" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="Submit" /></td> </tr> </table> </mvc:form> </body> </html>
因為我們把這個頁面放在了WEB-INF目次下,是以是不克不及直接經由過程URL對這個文件停止拜訪的,必需後面界說的“form.html”轉到controller處置後顯示這個視圖頁面,如許做的目標是避免一些私密的頁面在未受權的情形下被其別人隨便拜訪。在下面的文件中,須要留意的是:
(3)營業邏輯處置的controller類UserController.java:
package cn.zifangsky.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import cn.zifangsky.model.Gender; import cn.zifangsky.model.User; @Controller public class UserController { private static final String[] countries = {"China","Japan","North Korea","United States"}; @RequestMapping(value="/form.html") public ModelAndView user(){ ModelAndView modelAndView = new ModelAndView("userForm"); modelAndView.addObject("user", new User()); modelAndView.addObject("genders",Gender.values()); modelAndView.addObject("countries", countries); return modelAndView; } @RequestMapping(value="/result.html") public ModelAndView processUser(@ModelAttribute(value="user") User u){ ModelAndView modelAndView = new ModelAndView("userResult"); modelAndView.addObject("u",u); return modelAndView; } }
可以看出,在下面界說了兩個辦法,它們的感化分離是針對“form.html”要求轉到真實的form表單和對form表單的處置。在對表單處置時經由過程@ModelAttribute注解吸收了一個User類型的“u”,也就是後面填寫的form表單,前面就是表單的顯示是以不多說
(4)測試:
①表單填寫:
②成果顯示:
userResult.jsp頁面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="mvc"%> <!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>Spring MVC Form Handling</title> </head> <body> <h2>注冊成果</h2> <table> <tr> <td>姓名:</td> <td>${u.name}</td> </tr> <tr> <td>暗碼:</td> <td>${u.password}</td> </tr> <tr> <td>任務:</td> <td>${u.job}</td> </tr> <tr> <td>誕辰:</td> <td>${u.birthDate}</td> </tr> <tr> <td>性別:</td> <td>${u.gender}</td> </tr> <tr> <td>棲身地:</td> <td>${u.country}</td> </tr> <tr> <td>抽煙嗎:</td> <td>${u.smoking}</td> </tr> </table> </body> </html>
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。