We want to implement a feature like this:
Write two forms and commit to different processing methods in the same action. such as registration and login, are submitted to useraction this control class. However, these two submissions are handled by different methods of the Useraction control class.
The case structure is as follows:
The documents used in this case are: 1. Useractio.java (Control Class) 2.spring.xml (total configuration file) 3.springmvc_006.xml (a profile unique to this project) 4.adduser.jsp (JSP page with two forms)
The first step: Write the Web. xml file.
<?XML version= "1.0" encoding= "UTF-8"?><Web-appXmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"ID= "webapp_id"version= "2.5"> <Display-name>Springmvc_10day_self</Display-name> <!--Spring provides a filter specifically designed to solve the garbled problem of post submission Chinese - <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> <servlet> <!--This name can be obtained casually, but after the name has been taken, and later in the Web-inf to create the SPIRNGMVC configuration file is, the name must begin with this, so here is called Dispatcherservlet, Then the XML file must be named Dispatcherservlet-servlet.xml (not a single word) - <Servlet-name>Dispatcherservlet</Servlet-name> <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class> <!--Notify Dispatcherservlet to locate the Springmvc.xml configuration file in the specified directory - <!--Note that the <param-name>contextConfigLocation</param-name> here can not be wrong, and if there is a mistake, go to Web-inf and find it . - <Init-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:spring.xml</Param-value> </Init-param> </servlet> <servlet-mapping> <Servlet-name>Dispatcherservlet</Servlet-name> <Url-pattern>*.action</Url-pattern> </servlet-mapping> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list></Web-app>
Step two: Write the Spring.xml file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0 . xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context- 3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-3.0.xsd/HTTP Www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "><ImportResource= "Com/guigu/shen/action6/springmvc_006.xml"/></Beans>
Step three: Write the com/guigu/shen/action6/springmvc_006.xml file
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0 . xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context- 3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX http://www.springframework.org/schema/tx/spring-tx-3.0.xsd/HTTP Www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!--Controller (programmer) (must be configured)
With such a configuration, the system will go com.guigu.shen.Action6 the package to find the class with the annotated @controller.
-<Context:component-scanBase-package= "Com.guigu.shen.Action6"/> <!--<bean name= "/hello.action" class= "com.guigu.shen.Action5.HelloAction" ></bean> - <!--Annotation-based mapper (optional) This class differs from the previous XML class, specifically for annotations - <Beanclass= "Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!--annotation-based adapters (optional) This class differs from the previous XML class, specifically for annotations - <Beanclass= "Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!--View resolver (optional) This class is the same class as the previous XML method - <Beanclass= "Org.springframework.web.servlet.view.InternalResourceViewResolver"> </Bean></Beans>
Fourth step: Write the control class Useraction.java.
PackageCom.guigu.shen.Action6;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.ui.Model;Importorg.springframework.web.bind.annotation.RequestMapping;/*** * Request path can be split into: Root Module name + sub-module name is equivalent to when access to http://127.0.0.1:8080: project name/user/register will enter the Registermethod method. */@Controller @requestmapping (value= "/user")//request name of the root module Public classuseraction {/** Employee Registration **/@RequestMapping (Value= "/register")//request name of the sub-module PublicString Registermethod (model model) {Model.addattribute ("Message", "Employee registration successful"); return"/jsp/success.jsp";}
/ * * Employee login * */
@RequestMapping (Value= "/login")// sub-module request name public String Loginmethod (Model Model) { Model.addattribute ("message", "Employee Login Success"); return "/jsp/success.jsp"; }}
Sixth step: Test input hhttp://127.0.0.1:8080/springmvc_10day_self/adduser.jsp
Appear
Press the Register button. Will enter into the http://127.0.0.1:8080/SpringMvc_10day_self/user/register.action. This executes the public String Registermethod (model) method inside the control class useraction.
Press the login button. Will enter into the http://127.0.0.1:8080/SpringMvc_10day_self/user/login.action. Thus executing the control class useraction inside the
Public String Loginmethod (model model)
Method.
Summary: path through module root path + function Subpath = Access Module sub-function
11springmvc_ a action, write several similar business control methods