12SPRINGMVC_ write common variable collection parameters in the Business control method

Source: Internet
Author: User

This article is about the JSP page does not pass some parameters into the action, then the action how to accept this data?

Scheme:

The case structure is as follows:

With the previous blog case, just make some changes on the above: the case used in the file is: 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) -<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. (This step is also the core of this article)

 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/** Focus on the parameter names I write here. String username,string Salary The name of the Username,salary and * form submitted here is identical. The magic is that as long as the guarantee is exactly the same, you can collect the values on the page, * This is also SPRINGMVC and Struts2 very different point, spring is based on parameters to collect data. Struts2 is based on the entity to collect data. The cause of this problem is SPRINGMVC inside the action (Controller class is a singleton mode), if we adopt and STRUTS2 * In a way, inside define an EMP emp so, EMP only one, such as by 80 people submit this request to execute this method, * Will cause the 80th person to replace the previous person's data. So springmvc do so, 80 people do 80 methods, you can save every time * data. *  *  */ PublicString Registermethod (Model model,string username,string salary) {Model.addattribute ("Message", "Employee registration successful"); /** The username parameter is assigned as long as it is the same as the Name property on the page. * Salary*/System.out.print ("Information of the employees" +username+ ";" +salary); return"/jsp/success.jsp";} @RequestMapping (Value= "/login")//request name of the sub-module PublicString Loginmethod (Model model,string username) {Model.addattribute ("Message", "Employee registration successful");    SYSTEM.OUT.PRINTLN (username); return"/jsp/success.jsp"; }}

Sixth step: Test input hhttp://127.0.0.1:8080/springmvc_10day_self/adduser.jsp

Appear

Enter: Name (years later) and salary (100). 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. Typing on the console

employee information after the year;

--------------------------------------------------------------------------------------------------------------- ------------------------

But the above method obviously has a disadvantage. If I have a lot of parameters on my JSP page. Not only the name, the salary, but also the age ah what, it is not in the action of the Treatment method

To write a lot of parameters ah. How to solve? In the later article again.

12SPRINGMVC_ write common variable collection parameters in the Business control method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.