SpringMVC處理Form表單實(shí)例
Spring MVC 表單處理例子下面的例子說(shuō)明了如何編寫一個(gè)簡(jiǎn)單的基于 web 的應(yīng)用程序,它利用了使用 Spring 的 Web MVC 框架的 HTML 表單。
一 測(cè)試項(xiàng)目搭建
(1)新建Java Web項(xiàng)目,并引入幾個(gè)SpringMVC項(xiàng)目所需要的jar包,項(xiàng)目結(jié)構(gòu)和所需要的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結(jié)尾的url后綴并進(jìn)行處理
②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 />則啟動(dòng)了SpringMVC的一些默認(rèn)配置。在配置文件的最后則定義了邏輯視圖到實(shí)際視圖之間的對(duì)應(yīng)關(guān)系,一句話解釋就是:給返回的邏輯視圖加上上面定義的路徑前綴和后綴就是實(shí)際視圖的真正路徑了。
二 使用SpringMVC處理Form表單
(1)在正式開始之前,先建立一個(gè)model和枚舉類:
①實(shí)體類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; }
下面將依照程序的執(zhí)行流程來(lái)簡(jiǎn)單說(shuō)明SpringMVC的Form表單處理,分別是前臺(tái)的form表單填寫 –>controller處理 –>處理結(jié)果視圖頁(yè)面
(2)測(cè)試項(xiàng)目的首頁(yè)與form表單頁(yè)面:
①首頁(yè)index.jsp:
<% response.sendRedirect("form.html"); %>
可以看出,在這里我們的首頁(yè)很簡(jiǎn)單,就是重定向到“form.html”,但是通過(guò)我們前面在web.xml中的配置,SpringMVC將會(huì)對(duì)這個(gè)請(qǐng)求轉(zhuǎn)到一個(gè)具體的controller中進(jìn)行處理,當(dāng)然這里就是直接轉(zhuǎn)到form表單頁(yè)面。具體的controller里的處理邏輯下面再說(shuō)
②form表單頁(yè)面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>用戶注冊(cè)</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>
由于我們把這個(gè)頁(yè)面放在了WEB-INF目錄下,因此是不能直接通過(guò)URL對(duì)這個(gè)文件進(jìn)行訪問(wèn)的,必須前面定義的“form.html”轉(zhuǎn)到controller處理后顯示這個(gè)視圖頁(yè)面,這樣做的目的是防止一些私密的頁(yè)面在未授權(quán)的情況下被其他人隨意訪問(wèn)。在上面的文件中,需要注意的是:
- 為了簡(jiǎn)化form表單的寫法,因此引入了SpringMVC的表單標(biāo)簽庫(kù),也就是文件頂部的:<%@taglib uri=”http://www.springframework.org/tags/form” prefix=”mvc”%>
- modelAttribute表示手動(dòng)綁定了一個(gè)名為“user”的實(shí)體類,該值與controller中處理轉(zhuǎn)到這個(gè)form表單時(shí)設(shè)置的那個(gè)model值相對(duì)應(yīng)
- 表單中的path特性則是實(shí)現(xiàn)了對(duì)model的綁定,如:<mvc:input path=”name” />將該輸入值設(shè)置成model類中的“name”屬性。如果沒有顯式指定id和name屬性,那么在頁(yè)面中呈現(xiàn)的HTML input標(biāo)簽就會(huì)使用path特性來(lái)設(shè)置它的id和name屬性
(3)業(yè)務(wù)邏輯處理的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; } }
可以看出,在上面定義了兩個(gè)方法,它們的作用分別是針對(duì)“form.html”請(qǐng)求轉(zhuǎn)到真實(shí)的form表單以及對(duì)form表單的處理。在對(duì)表單處理時(shí)通過(guò)@ModelAttribute注解接收了一個(gè)User類型的“u”,也就是前面填寫的form表單,后面就是表單的顯示因此不多說(shuō)
(4)測(cè)試:
①表單填寫:
②結(jié)果顯示:
userResult.jsp頁(yè)面:
<%@ 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>注冊(cè)結(jié)果</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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- layui 圖片上傳+表單提交+ Spring MVC的實(shí)例
- SpringMVC實(shí)現(xiàn)表單驗(yàn)證功能詳解
- springMVC中基于token防止表單重復(fù)提交方法
- SpringMVC中使用bean來(lái)接收f(shuō)orm表單提交的參數(shù)時(shí)的注意點(diǎn)
- SpringMVC實(shí)現(xiàn)數(shù)據(jù)綁定及表單標(biāo)簽
- Spring MVC---數(shù)據(jù)綁定和表單標(biāo)簽詳解
- [Spring MVC] -簡(jiǎn)單表單提交實(shí)例
- Spring MVC中基于自定義Editor的表單數(shù)據(jù)處理技巧分享
- SpringMVC表單提交參數(shù)400錯(cuò)誤解決方案
相關(guān)文章
SpringCloud?Gateway實(shí)現(xiàn)請(qǐng)求解密和響應(yīng)加密的過(guò)程解析
這篇文章主要介紹了SpringCloud?Gateway實(shí)現(xiàn)請(qǐng)求解密和響應(yīng)加密的相關(guān)知識(shí),本文環(huán)境使用比較新的?Java?17?和?SpringBoot?3.1.5,對(duì)應(yīng)到Spring的版本是?6.0.13,本文重心是網(wǎng)關(guān)項(xiàng)目,需要的朋友可以參考下2023-11-11Spring cloud Gateway簡(jiǎn)介及相關(guān)配置方法
這篇文章主要介紹了Spring cloud Gateway簡(jiǎn)介及相關(guān)配置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04IntelliJ IDEA中使用mybatis-generator的示例
這篇文章主要介紹了IntelliJ IDEA中使用mybatis-generator,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04SpringBoot中使用Servlet的兩種方式小結(jié)
這篇文章主要介紹了SpringBoot中使用Servlet的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07