Spring passes values to the page and accepts detailed explanations of parameters passed from the page
This article mainly introduces the related contents of spring passing values to the page and accepting parameters passed from the page for your reference and learning. Let's take a look at the detailed introduction below:
1、 Receive parameters from page
Several methods for spring MVC to receive parameter values submitted by requests:
1. Get using HttpServletRequest.
2. Use @ requestparam annotation.
3. It is encapsulated into entity parameters using automatic mechanism.
2、 Pass value to page
When the controller component needs to transfer values to the response JSP after processing, the following methods can be used:
1) , use HttpServletRequest and session, and then setAttribute (), just as in servlet
The model data will use the attribute of HttpServletRequest to pass the value to success JSP
2) , using modelandview objects
3) , using modelmap objects
Example of using modelmap parameter object:
Modelmap data will use the attribute of HttpServletRequest to pass the value to success JSP
4) , use the @ modelattribute annotation
Using the @ modelattribute example
Using @ modelattribute data on the parameter part of the controller method or the bean attribute method will use the attribute of HttpServletRequest to pass the value to success JSP
5) Session storage: you can use the getsession () method of HttpServletRequest
6) Custom map
7) Spring MVC uses forwarding to locate the view by default. If you want to use redirection, you can do the following
1. Use redirectview
2. Use redirect: prefix
Or use the following methods, which are commonly used in work:
3、 Example explanation:
Step 1: create a new web project and import spring MVC package and business layer userservice
1. Create a web project and import related jar packages:
2. Import the above business layer userservice class and dependent classes, etc.
The user class code is as follows:
Userdao interface code is as follows:
The userservice class code is as follows:
The nameorpwdexception class code is as follows:
Nullparamexception class code is as follows:
The jdbcdatasource class code is as follows:
The code of mysqluserdao class is as follows:
db. The contents of the properties file are as follows:
spring-mvc. The XML file code is as follows:
The SQL code of MySQL database initialization is as follows:
3. Add junit4 API for the project, and then add test class testcase and test method testuserservice () to test whether the above configuration is correct. The testcase class code is as follows:
Execute the test method testuserservice(), and output the results on the console:
Using JDBC technology to find user information
This result shows that the business layer userservice works normally.
4. Configure the spring MVC core controller dispatcherservlet to the web xml。 web. The code reference of XML configuration part is as follows:
5. Deploy the project to Tomcat and start it. Test whether the spring MVC configuration is normal.
If there is content in the output result and there is no exception, it will indicate that the spring MVC deployment is normal.
Step 2: implement login-action1 Form login process, test the value transfer method using httpservletrequrst
1. Add login form under WEB-INF / JSP folder JSP file, the code is as follows:
Method 1 explanation:
The HttpServletRequest object is used to transfer values from the browser page to the controller.
Method 2 explanation:
Use the @ requestparam annotation to declare that the value of PWD in the form password input box is injected into the password variable, and the user name input box in the form is automatically injected into the name variable according to the name mapping@ The requestmapping annotation will login-action2 Form is mapped to the checklogin2 () method. This is the same as login - form JSP form correspondence.
Method 3 explanation:
Here, user is used as the parameter. Spring will automatically inject the page form parameters name and PWD into the corresponding attributes name and PWD of the user object and pass them to the method@ Requestmapping will request login-action3 Form is mapped to the method checklogin3().
Method 4 explanation:
Returns a modelandview object after the processing method is completed.
Method 5 explanation:
Modelmap attribute value and page login form Data correspondence between JSPS.
Method 6 explanation:
@The attributes declared by modelattribute are the same as login form The value relationship of the JSP page.
When the user name or password is wrong, you can remember the user name.
In the previous case, after the user logs in successfully, you can use the getsession () method of the HttpServletRequest object to access the session object, so that the user can maintain the login status.
2. Add a style file styles. For the page CSS, the style file is saved in the Webroot folder, styles The CSS file code is as follows:
3. Add success.jsp in the WEB-INF / JSP folder JSP file, which is the interface displayed after successful login. The code is as follows:
In the above page, El expressions and standard tag libraries are used to display interface data, where < C: URL var = "base" value = "/ > < / C: URL > and ${base} are used to solve the absolute path problem.
4. Create a controller class logincontroller, and configure it by annotating @ controller in this class:
1) Use @ controller to declare logincontroller as a controller bean component.
2) Use @ requestmapping ("/ login") to declare that the request for the logincontroller component is under the / login path.
3) The process control method loginform() is used to display the login form page. Use the @ requestmapping annotation to map the request / login form Form to loginform() method.
4) Add the userservice attribute and use the @ resource annotation declaration to inject the userservice object during runtime.
5) Add the control process method checklogin1 (), and use the @ requestmapping annotation to request / login-action1 Form is mapped to the checklogin1 () method. The checklogin1 () method calls the login method of userservice to implement the login process. The checklogin1 () method is mainly used to test the data transmission mode from the JSP page to the controller.
The logincontroller class code is as follows:
5. Test login-action1 Form login process
Via web site“ http://localhost:8080/SpringValues/login/login.form ”Request Tomcat server:
The information of the access request is printed on the console:
summary
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.