SPINGMVC uses an instance of the form label for the form.
First, the project structure
Two, the required jar package
Commons-logging.jar
Spring-aop-4.1.8.release.jar
Spring-beans-4.1.8.release.jar
Spring-context-4.1.8.release.jar
Spring-core-4.1.8.release.jar
Spring-expression-4.1.8.release.jar
Spring-web-4.1.8.release.jar
Spring-webmvc-4.1.8.release.jar
The following two are required for the JSTL tag library
Standard.jar
Jstl.jar
Third, Web. XML Configuration Springmvc Dispatcherservlet
The main dispatcherservlet configuration and initialization of the Springmvc.xml configuration file is in Web. Xml.
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:javaee= "Http://java.sun.com/xml/ns/javaee" xmlns:web= "http// Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "xsi:schemalocation=" HTTP://JAVA.SUN.COM/XML/NS/J2EE/http Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "id=" webapp_id "version=" 2.4 "> <!--configuration Disptatcherservlet--< Servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--configuration Dispatcherservlet initialization Parameters-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath: Springmvc.xml</param-value> </init-param> < Create a servlet-to-<load-on-startup>1 when the web app is loaded!-- </load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet< /servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <we Lcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Four, springmvc.xml configuration
Configure scan package files, view resolvers, and so on.
<?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:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" Xsi:schema location= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans- 4.1.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context /spring-context-4.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/aop/spring-aop-4.1.xsd Http://www.springframework.org/schema/tx http://www.springframework.org /schema/tx/spring-tx-4.1.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1 . xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/sp Ring-context-4.1.xsd "> <!--Configure automatic scanning of packages--<context:component-scan base-package=" Com.lanhuigu.springmvc " /><!--View Resolver--<bean id= "Viewresolver" class= "Org.springframework.web.servlet.view.InternalR Esourceviewresolver "> <!--configuration prefix--<property name=" prefix "value="/web-inf/views/"/> <!--configuration suffix--<property name= "suffix" value= ". jsp"/> </bean> <!--custom View resolver: B Eannameviewresolver using the view's name resolution View--<bean class= "Org.springframework.web.servlet.view.BeanNameViewResolver" > <!--Set the priority value of the Custom view resolver beannameviewresolver and Internalresourceviewresolver view resolver, Internalresourceviewresolv The order default value for ER is the maximum value of integer, BeannamEviewresolver the lower the order for the 100,order value, the higher the view resolver's precedence. The Custom view resolver Beannameviewresolver in this profile takes precedence over Internalresourceviewresolver, using Beannameviewresolver to parse the view first, and if the need is not implemented, Automatically calls the Internalresourceviewresolver parse view. --<property Name= "order" > <value>100</value> </property> </bean> < ;! --Configure Direct Forwarding page--<mvc:view-controller path= "/success" view-name= "Success"/> <!--resolve Mvc:view-controller configuration Post requestmapping map address 404 problem-<mvc:annotation-driven></mvc:annotation-driven></beans>
Five, Entity class user and address
User.java
Package Com.lanhuigu.springmvc.entities;public class User {private string Username;private string password;private Integer gender;private Address address;//cascading class public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public Integer Getgender () {return gender;} public void Setgender (Integer gender) {This.gender = gender;} Public Address getaddress () {return address;} public void setaddress (address address) {this.address = address;} /** * Override ToString () method */@Overridepublic String toString () {return "User [username=" + Username + ", password=" + password + ", gender=" + Gender + ", address=" + address+ "]";}}
Address.java
Package Com.lanhuigu.springmvc.entities;public class Address {private Integer id;private string Province;private string City;public Integer getId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String getprovince () {return province;} public void Setprovince (String province) {this.province = province;} Public String getcity () {return city;} public void Setcity (String city) {this.city = city;}}
Six, RESTful style action, file under Action package
There are three methods in the control class and the data are simulated:
(1) Querylist query user list
(2) Editinput Initial add user's page
(3) Add user method, there is no database connection, the main purpose is to view the form form submitted by the data
Package Com.lanhuigu.springmvc.action;import Java.util.arraylist;import Java.util.list;import java.util.Map;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Com.lanhuigu.springmvc.entities.address;import Com.lanhuigu.springmvc.entities.User, @RequestMapping ("/testrestful") @Controllerpublic class Testrestfulaction {/* * Query list data * @param map * @return */@RequestMapping ("/list") public String querylist (map<string, object> map) {LIST&L T user> lists = new arraylist<user> (); User user = new user (), User.setusername ("one"), User.setpassword ("111111"); Address address = new address (), Address.setprovince ("aaaaaa"), Address.setcity ("bbbbbb"); user.setaddress (address); Lists.add (user); Map.put ("Users", lists); return "List";} /** * Initialize edit page * @param map * @return */@RequestMapping (value= "/input", method=requestmethod.get) public String editinput ( Map<string, OBJECT> Map) {map.put ("user", new user ()); return "Input";} /** * Add User * @param user * @return */@RequestMapping (value= "/adduser", method=requestmethod.post) public String Add (user us ER) {System.out.println ("Username:" +user.getusername () + "+" Password: "+user.getpassword () +" + "Gender:" +USER.GETG Ender () + "" + "province:" +user.getaddress (). Getprovince () + "+" City: "+user.getaddress (). getcity ()); return" redirect :/testrestful/list ";}}
Seven, list.jsp page, the page is located under Web-inf under the views
This page is a list display page, there is an add user on the page to add a hyperlink for users to initialize the user to add a page
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <%@ taglib prefix= "C" uri= "/HTTP/ Java.sun.com/jsp/jstl/core "%><%@ taglib prefix=" form "uri=" Http://www.springframework.org/tags/form "%> <% String Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > input.jsp initialization Editing page, located under the views, location with list.jsp, the contents are as follows:
Add the user's page, the map Code of the page can be in fact through the database query encapsulation, or model data definition, and so on, here directly through the Java code on the JSP page.
The page is submitted to AddUser by post to the action address, Modelattribute is the form property, it automatically binds from a property value in model to the entity object of the current form.
The default is the command property, error when command is not found, we manually encapsulate an empty user override. Spring form submission Method In addition to Get,post and Delete,put.
<% @page import= "com.lanhuigu.springmvc.entities.Address"%><%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix= "Form" uri= "http://www.springframework.org/tags/form"%><% String Path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Eight, access address test
Test logic: Through the first address to the user list page, click on the add user is added users initialization page, complete the page to fill the submission form,
Console view or debugger tracking to see the results of the submission, experience the Spring form form in SPRINGMVC.
Http://localhost:9000/SpringMVC/testRestful/list to the list display page:
To add User Form page, click Add Users:
Submit the form, view the console, or under the Input method debugger see if user gets the value of the form page submission
Nine, console output results
Ten, summary analysis
A single diagram is listed for the form table:
The use of this tag is similar to the one used in HTML, the attributes of individual tags are different, but they are all as the name implies.
SPRINGMVC (------) Use of form labels