In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to implement the back-end controller in spring-mvc, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
1. Overview of SpringMVC back-end Controller
In order to facilitate developers to quickly build back-end controllers suitable for specific applications, springMVC implements the Controller interface and customizes many specific controllers. The hierarchical relationships of these controllers are as follows:
-AbstractController
-AbstractUrlViewController
-UrlFilenameViewController
-BaseCommandController
-AbstractCommandController
-AbstractFormController
-AbstractWizardFormController
-SimpleFormController
-CancellableFormController
-MultiActionController
-ParameterizableViewController
-ServletForwardingController
-ServletWrappingController
The following focuses on two characteristic controllers:
2.SimpleFormController controller
Before formal development, please familiarize yourself with the previous HelloWord instance. After ensuring that we are familiar with the previous example, we set up a project called springMVC_02_controllerweb and import the relevant jar package.
Step 1: create the back-end controller RegControl.java code as follows:
Java code
Package com.asm; / /... Omit the imported related class public class RegControl extends SimpleFormController {@ SuppressWarnings ("deprecation") public RegControl () {setCommandClass (User.class);} protected ModelAndView processFormSubmission (HttpServletRequest arg0, HttpServletResponse arg1, Object formbean, BindException arg3) throws Exception {User user = (User) formbean; ModelAndView mav = new ModelAndView ("hello") Mav.addObject ("message", "Hello World!"); mav.addObject ("user", user); return mav;} protected ModelAndView showForm (HttpServletRequest arg0, HttpServletResponse arg1, BindException arg2) throws Exception {return null;}}
User.java, the code is as follows:
Java code
Package com.asm; public class User {private String username; private int age; / / omit getter/setter method}
Brief description: if you are familiar with struts1.x, I believe it is easy to understand Object formbean parameters, in fact, it is an object that deals with form properties, that is, form parameters will be populated to formbean objects according to certain rules. In struts1.x, if you convert this and formbean into User objects, you must require User to inherit from the ActionForm class, so that a form parameter can be converted into a specific formbean object (the so-called concrete essence means that the parameter formbean object has been successfully assigned to the User object) and bound with the corresponding Action. But springmvc does not require that this kind of User must inherit a certain class. Since springmvc does not require this kind of User, how do the form parameters match with User? note that there is the following code in the RegControl construction method: setCommandClass (User.class); this code indicates that the controller binds the User class to match the form. If you want to verify the role of this code, you can comment out the code and look at the exception. An execution process of this controller (including form filling and validation) will be analyzed later.
Summarize the main points of this step: (1) inherit the SimpleFormController class (2) call the setCommandClass method in the constructor to bind the command object (here, the User class) (3) convert formbean to the User class for business logic operations
Step 2: configure web.xml (same as the previous HelloWorld instance, omitted here)
Step 3: configure the spmvc-servlet.xml file as follows:
Xml code
Bean > regControlprop > props > property > bean > bean >
Step 4: improve the corresponding page according to the configuration file
On the index.jsp page, the main code is as follows:
User name: age: form >
/ page/hello.jsp, the main code is as follows:
Hello, World! (WEB-INF/page) user name: ${user.username} Age: ${user.age} body >
Step 5: start the server, visit the home page, and fill out the form to complete the test.
3. Study SimpleController controller in detail
Add the following code to RegControl.java:
Java code
Protected Object formBackingObject (HttpServletRequest request) throws Exception {System.out.println ("formBackingObject method execution-- > 01"); setCommandClass (User.class); / / you can also call setCommandClass method return super.formBackingObject (request) here;} protected void initBinder (HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {System.out.println ("initBinder method execution-- > 02") Super.initBinder (request, binder);} protected void onBind (HttpServletRequest request, Object command) throws Exception {System.out.println ("onBind method execution-- > 03"); super.onBind (request, command) } protected void onBindAndValidate (HttpServletRequest request, Object command, BindException errors) throws Exception {System.out.println ("onBindAndValidate method execution-- > 04"); super.onBindAndValidate (request, command, errors);}
The following is a brief analysis of the implementation process:
(1)。 After the current controller transfers the request to this controller, the formBackingObject method is called first. The purpose of this method is to create a Command object based on the bound CommandClass, so you can call the setCommandClass method as well as the setCommandClass method in the constructor. In fact, it is very simple to create this Command object, and spring is done with the following code:
BeanUtils.instantiateClass (this.commandClass)
Since the creation of the Command object must be done according to commandClass here, we should ensure that the commandClass setting is complete before calling this method, so we can complete the commandClass setting in the formBackingObject method and constructor.
(2)。 Call the initBinder method to initialize the Command object, that is, assign the form parameters to the Command field by name matching.
(3)。 Call the onBind method to bind the Command object to the back-end controller.
(4)。 Call the onBindAndValidate method to verify that the data entered by the user is legal. If the validation fails, we can modify the errors parameter, that is, the new errors object will be bound to the ModelAndView and return to the form fill-out page.
(5)。 To execute the processFormSubmission method, the main operation is to convert the bound Command object into a form object such as User, and call the business logic method to operate the User object, returning different ModelAndView objects according to different logic.
4.MultiActionController controller
This controller combines multiple request processing methods into a single controller so that related functions can be combined (it is very similar to DispatchAction in struts1.x). The use of this controller is demonstrated by an example.
Step 1: under the springMVC_02_controllerweb project, create the backend controller UserManagerController.java as follows:
Java code
Package com.asm; / /... Omit imported related classes public class UserManagerController extends MultiActionController {public ModelAndView list (HttpServletRequest request, HttpServletResponse response) {ModelAndView mav = new ModelAndView ("list"); return mav;} public ModelAndView add (HttpServletRequest request, HttpServletResponse response) {ModelAndView mav = new ModelAndView ("add"); return mav } public ModelAndView edit (HttpServletRequest request, HttpServletResponse response) {ModelAndView mav = new ModelAndView ("edit"); return mav;}}
Step 2: configure web.xml (see the previous example), and add the following configuration to spmvc-servlet.xml:
Xml code
Listprop > addprop > editprop > props > property > bean > property > bean >
Description: methodNameResolver is responsible for parsing the name of the method to be called from the request. Spring itself already provides a series of MethodNameResolver implementations, but you can also write your own. Here, we use the Pro method to parse, as shown below:
Call the list method when list requests list.do
Call the add method when the add request is add.do
Call the edit method when the edit request is edit.do
Then, by injecting the springMethodNameResolver parser into UserManagerController's methodNameResolver, a real MultiActionController controller object with request forwarding capability is completed after configuration-- UserManagerController emphasizes that this step essentially completes the job of configuring a method parser for the UserManagerController controller.
Step 3: configure the access path for request forwarding and add the following code to spmvc-servlet.xml
Xml code
UserManagerControllerprop > props > property > bean >
Step 4: improve the jsp page writing according to the configuration file.
Page/list.jsp, the code is as follows:
User list page body >
Page/add.jsp, the code is as follows:
User add page body >
Page/edi.jsp, the code is as follows:
User modifies the page body >
Step 5: start the server and visit … / list.do will call the list method and go to the list.jsp page.
Add: elaborate on MethodNameResolver parser
InternalPathMethodNameResolver: the default MethodNameResolver parser, which gets the file name from the request path as the method name. For example,... The / list.do request calls the list (HttpServletRequest,HttpServletResponse) method.
ParameterMethodNameResolver: parses the request parameter and uses it as the method name. For example, corresponding to... The / userManager.do?method=add request calls the add (HttpServletRequest, HttpServletResponse) method. Use the paramName attribute to define the name of the request parameter to use.
PropertiesMethodNameResolver: use a user-defined attribute (Properties) object to map the requested URL to the method name, as shown in the example.
When using ParameterMethodNameResolver as the parser for MethodNameResolver, the main configuration code is as follows:
Xml code
UserManagerControllerprop > props > property > bean > property > bean > property > bean >
The access path is … / user.do?crud=list (add | edit)
These are all the contents of the article "how to implement the back-end Controller in spring-mvc". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.