In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to implement the mapping processor in spring-mvc, which is very detailed and has a certain reference value. Friends who are interested must read it!
1. A brief Analysis of Mapping processor
In spring mvc, web requests can be mapped to the correct processor using a mapping processor. Spring has a lot of built-in mapping processors, and we can customize the mapping processors. The following example shows the two most commonly used mapping processors in spring: BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping. It is necessary to understand the following relevant points before officially starting:
(1) the mapping processor can pass the request to the processor execution link (HandlerExecutionChain), and the processor execution link must contain a processor that can handle the request (in essence, this processor is dynamically added to the processor chain, which can be understood in combination with the working principle of filter), and the processor link can also contain a series of interceptors.
(2) the two most commonly used processors in spring listed above inherit from the AbstractHandlerMapping class, so they have the properties of the parent class.
two。 Example: BeanNameUrlHandlerMapping
Set up the springMVC_03_handlerMappingsweb project and import the relevant jar package.
Step 1: create a back-end controller MessageController.java with the following code:
Java code
Package com.asm; / /... Omit imported related classes public class MessageController implements Controller {public ModelAndView handleRequest (HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {ModelAndView mav = new ModelAndView ("message"); mav.addObject ("message", "Hello! spring MVC"); return mav;}}
Step 2: configure web.xml, the code is as follows:
Xml code
"2.4" xmlns= "http://java.sun.com/xml/ns/j2ee" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> spmvc class > Org.springframework.web.servlet.DispatcherServlet class > 1 spmvc * .do
Step 3: configure spmvc-servlet.xml. The code is as follows:
Xml code
"http://www.springframework.org/schema/beans" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context=" http://www.springframework.org/schema/context" xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema / beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> "viewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver" > "prefix" value= "/ WEB-INF/page/" / > "suffix" value= ".jsp" / > "/ message.do" class= "com.asm.MessageController" >
Step 4: create a message.jsp under the WEB-INF/page directory. The main code is as follows:
Message:$ {message} body >
Step 5: start the server and enter … / message.do access test.
A brief Analysis of the implementation process
(1) after starting the server, when we send the message.do request to the server, we are first intercepted by the front-end controller DispatcherServlet configured in web.xml.
(2) the front-end controller transfers the request to the back-end controller, and the transfer process is analyzed as follows: when looking for a mapping processor in spmvc-servlet.xml that can execute the message.do request, it is found that there is no mapping processor that can handle the request, then the default mapping processor BeanNameUrlHandlerMapping:This is the default implementation used by the DispatcherServlet, along with DefaultAnnotationHandlerMapping (on Java 5 and higher) is used. We should also note that the bean Name of this back-end controller must start with "/" and be combined with the mapping configuration of DispatcherServlet. At the same time, beanName supports wildcard configuration. For example, if you configure:, you can also successfully access the MessageController class when accessing messasge.do.
(3) the BeanNameUrlHandlerMapping processor looks up whether there is a bean instance named "message.do" in the spring container. When the instance is found, the bean is used as the back-end controller to process the request. At the same time, add yourself to the mapping processor chain and pass the request to the processor chain.
(4) the back-end controller processes and returns to the view.
3. Example: SimpleUrlHandlerMapping
Step 1: establish the back-end controller UserContrller.java. The code is as follows:
Java code
Package com.asm; / /... Omit imported related classes public class UserController extends SimpleFormController {protected ModelAndView processFormSubmission (HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {System.out.println ("call logic layer to process forms"); ModelAndView mav = new ModelAndView ("loginSuc"); return mav;}}
Step 2: add the following configuration to spmvc-servlet.xml:
Xml code
List > property > userControllerprop > props > property > bean > bean > bean >
Description: (1) through the previous example, we can know that the back-end controller such as SimpleController must be bound to a commandClass object, here we bind through the configuration file.
(2) userController configuration indicates that as long as the access starts with op, the middle * can be any character, and ends with login.do, the userController controller can be accessed.
(3) SimpleUrlHandlerMapping is a more powerful mapping processor that supports Ant-style path matching in addition to the above configuration. You can also configure it in the following form:
/ op/*/login.do=userController value > property >
(4) interceptor: in order to provide special functions for some special requests, spring provides interceptor support for mapping processors. Its configuration file is simple: one is to bring the interceptor class into the spring container management, and the other is to introduce the configured interceptor bean in the mapping processor.
Step 3: write the interceptor LoginTimeInterceptor.java. The main code is as follows:
Java code
Package com.asm; / /... Omit the imported related class public class LoginTimeInterceptor extends HandlerInterceptorAdapter {private int startTime; private int endTime; public void setStartTime (int startTime) {this.startTime = startTime;} public void setEndTime (int endTime) {this.endTime = endTime } public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println ("execute afterCompletion method-- > 03"); super.afterCompletion (request, response, handler, ex) } public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println ("execute postHandle method-- > 02"); super.postHandle (request, response, handler, modelAndView) } public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("execute preHandle method-- > 01"); Calendar cal = Calendar.getInstance (); int hour = cal.get (Calendar.HOUR_OF_DAY) If (startTime index.jsp code: user name: password: form >
Step 5: visit the index.jsp page to complete the test.
Analyze the execution process: in order to clearly understand the entire processor execution process, we first add the following code to UserController.java:
Java code
Protected Object formBackingObject (HttpServletRequest request) throws Exception {System.out.println ("formBackingObject method execution-> 01"); return super.formBackingObject (request);} 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);}
(1) when visiting … / login.do is first intercepted by the front-end controller DispatcherServlet. The front-end controller looks up the spmvc-servlet.xml configuration file and hands it over to the back-end controller for processing.
(2) after execution, we get the following print result, and we know a general execution process from the print result.
Execute the preHandle method-- > 01
FormBackingObject method execution-- > 01
InitBinder method execution-- > 02
OnBind method execution-- > 03
OnBindAndValidate method execution-- > 04
Call the logic layer to process the form
Admin----123456
Execute the postHandle method-- > 02
Execute the afterCompletion method-- > 03
These are all the contents of the article "how to implement the Mapping processor in spring-mvc". Thank you for reading! Hope to share the content to help you, more related 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.