Spring MVC Form Processing Example The following example illustrates how to write a simple web-based application that leverages the HTML forms that use the Spring Web MVC framework.
A test project to build
(1) Create a new Java Web project and introduce the jar packages needed for several SPRINGMVC projects, the project structure and the required jar packages as follows:
①web.xml:
<web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xs i: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.springfra Mework.web.servlet.dispatcherservlet</servlet-class> <load-on-startup>1</load-on-startup> < /servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.h tml</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-v Alue> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern
>/*</url-pattern> </filter-mapping> </web-app>
This defines the URL suffix for SPRINGMVC interception with an. HTML end and handles it
②springmvc-servlet.xml:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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.springfra
Mework.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/> < !--activate annotations defined in the bean--> <mvc:annotation-driven/> <bean class= "org.springframework.web.servlet.view.In Ternalresourceviewresolver "> <propeRty name= "prefix" value= "/web-inf/pages/"/> <property name= "suffix" value= ". jsp"/> </bean> </bea Ns>
In the configuration file above, <context:annotation-config/> Activates some of the annotations defined in the bean, and <mvc:annotation-driven/> Some of the default configurations for SPRINGMVC are started. At the end of the configuration file, the corresponding relationship between the logical view and the actual view is defined: The logical view of the return plus the path prefix and suffix defined above is the true path to the actual view.
Two use SPRINGMVC to process form forms
(1) Establish a model and enumeration class before the formal start:
① entity class 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;
}
}
② enumeration class gender that represents "gender":
Package Cn.zifangsky.model;
public enum Gender {
MALE,
FEMALE;
}
The following procedure will be followed in accordance with the process of implementation of SPRINGMVC form form processing, respectively, the form of the front desk filling –>controller processing –> processing Results View page
(2) test items of the first page and form form pages:
① Home index.jsp:
<% response.sendredirect ("form.html"); %>
Can see, here our homepage is very simple, is redirects to "form.html", But with our previous configuration in Web.xml, SPRINGMVC will be able to move the request to a specific controller, and of course this is to go directly to the Form form page. The specific controller of the processing logic in the following talk
②form form page userform.jsp:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%> <% @taglib uri=" http://www.springframework.org/tags/form "prefix=" MVC "% >
Because we put this page in the Web-inf directory, so it is not directly through the URL to the file access, must be defined before the "form.html" to controller processing after the display of this view page, The goal is to prevent some private pages from being accessed by others without authorization. In the above file, you need to be aware of:
- In order to simplify the form of forms, the introduction of the Springmvc Form tag Library, which is the top of the file: <% @taglib uri= "Http://www.springframework.org/tags/form" prefix= " MVC "%>
- Modelattribute indicates that an entity class named "User" has been manually bound, which corresponds to the model that is set when processing is transferred to the form form in controller
- The path attribute in the form implements the binding to model, such as: <mvc:input path= "Name"/> Sets the input value to the ' name ' property in the Model class. If you do not explicitly specify an ID and name attribute, the HTML input tag that is rendered in the page uses the path attribute to set its ID and Name property
(3) Business logic processing of controller class 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 = {"The", "Japan", "North Korea", "the unit
Ed States "}; @RequestMapping (value= "/form.html") public Modelandview User () {Modelandview Modelandview = new Modelandview ("Userfo
RM ");
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) {Mod
Elandview Modelandview = new Modelandview ("Userresult");
Modelandview.addobject ("U", u);
return modelandview;
}
}
As you can see, there are two methods defined above, and their role is to go to the real form form and the processing of form forms for "form.html" requests. The "U" of the user type was received by the @modelattribute annotation when processing the form, which is the form form that was previously filled in, followed by the display of the form so not much.
(4) Testing:
① Form Fill in:
② Results show:
userresult.jsp page:
<%@ 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" >
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.