Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringMVC configuration XML

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The specific process steps of the core architecture are as follows: Download Address:

1. First, the user sends a request-->DispatcherServlet. After receiving the request, the front-end controller does not process it itself, but delegates it to other resolvers for processing. As a unified access point, it performs global process control.

2. DispatcherServlet-->HandlerMapping, HandlerMapping will map the request to HandlerExecutionChain object (including a Handler (page controller) object and multiple HandlerInterceptor interceptors) objects. Through this policy mode, it is easy to add new mapping policies;

DispatcherServlet-->HandlerAdapter, HandlerAdapter will package the processor as an adapter, thus supporting multiple types of processors, that is, the application of the adapter design pattern, so that it is easy to support many types of processors;

4. HandlerAdapter--> Processor function processing method call, HandlerAdapter will call the real processor function processing method according to the adaptation result, complete the function processing; and return a ModelAndView object (containing model data, logical view name);

ModelAndView logical view name--> ViewResolver, ViewResolver will resolve the logical view name into a specific View, through this strategy mode, it is easy to replace other view technologies;

6. View--> Rendering, View will render according to the model data passed in, where the Model is actually a Map data structure, so it is easy to support other view technologies;

7. Return control to DispatcherServlet, and DispatcherServlet returns a response to the user. This process ends.

1. DispatcherServlet;

2、 HandlerMapping

3、 HandlerAdapter

4、 ViewResolver

5. Processor/page controller

6. View

DispatcherServletDispatcherServlet is the core of SpringMVC. Register the following Servlet registration information in web.xml. When initializing DispatcherServlet, the framework looks for a file named [servlet-name]-servlet.xml in the web application WEB-INF directory and defines the relevant Beans there, overriding any Beans defined globally, like the code in web.xml above, corresponding to dispatcher-servlet.xml; of course, you can also use the element to manually specify the path to the configuration file;load-on-startup: indicates that the Servlet is initialized when the container is started;url-pattern: Indicates which requests are handled by Spring Web MVC, and the "/" is used to define the default servlet mapping. It can also mean "*.html" to block all requests with html extensions.

Dispatcher

org.springframework.web.servlet.DispatcherServlet

Load all XML in/WEB-INF/spring-mvc/directory as Spring MVC configuration file

contextConfigLocation

/WEB-INF/spring-mvc/*.xml

1

Dispatcher

"/"/"*.do"/"*.html"

[servlet-name]-servlet.xml Configuration:

-->

InternalResourceViewResolver Prefixes the view name returned by ModelAndView with the prefix attribute configuration, and then adds the suffix configured by the suffix attribute at the end. Since the ModelAndView returned by HelloController has the name welcome, InternalResourceViewResolver will look for the view at/WEB-INF/jsp/welcome.jsp.

1、BeanNameUrlHandlerMapping

This is the default HandlerMapping for Dispatcher Servlets, so simply defining the name of a controller Bean in the application context configuration file with a "Url style" tells the Dispatcher Servlet what style requests should be handled by which controller, without explicitly defining a HandlerMapping. (Privately, this is simple, but the name of the Controller Bean defined in URL style is odd because it is also the instance name of the Controller Bean)

Example: If the URL style of ListCoursesController is "listCourses.go", then

Of course, you can also explicitly declare the HandlerMapping you use before defining the controller Bean, explicitly defined as follows:

2、SimpleUrlHandlerMapping

This approach is different from BeanNameUrlHandlerMapping, where SimpleUrlHandlerMapping does not need to define the name of the Controller Bean, but maps the URL directly to the controller.

Since SimpleUrlHandlerMapping is not the default HandlerMapping for Dispatcher Servlets, this HandlerMapping must be explicitly defined.

A HandlerMapping Bean with ID "SimpleUrl" is defined as follows:

listCoursesController

loginController

3. Default HandlerMapping

Default execution Class name: For example, if there is a CoursesController here, then/courses will call this Controller multi-method/courses/method name

Configure a view parser to combine controllers with JSPs.

Add the parser configuration fragment to test-servlet.xml above.

InternalResourceViewResolver Prefixes the view name returned by ModelAndView with the prefix attribute configuration, and then adds the suffix configured by the suffix attribute at the end. Because the ModelAndView returned by HelloController has the name welcome, InternalResourceViewResolver will look for the view at/WEB-INF/jsp/welcome.jsp.

(1)Mapping of urls and actions (backend controllers).

Spring MVC has a concept of HandlerMapping. It's actually a processor mapping Bean that assigns a controller to a URL. Spring provides three useful implementations of HandlerMapping:

-- BeanNameUrlHandlerMapping

Map controller to URL based on controller name

-- SimpleUrlHandlerMapping

Map controllers to URLs with a collection of attributes defined in the context profile

-- CommonsPathMapHandlerMapping

Map controllers to URLs using metadata in controller code

(2)Mapping of logical view names and view objects.

Spring MVC also has a ViewResolver concept. It determines how the logical view name of the ModelAndView object resolves into a view Bean that is used to render the results to the user. Spring has four implementations of ViewResolver:

-- InternalResourceViewResolver

Resolves the logical view name into a view object rendered with template files such as JSP and Velocity templates

-- BeanNameViewResolver

Resolves a logical view name into a view Bean in the DispatcherServlet application context

-- ResourceBundleViewResolver

Resolves logical view names into view objects in a ResourceBundler

-- XmlViewResolver

Parse the view Bean from an XML file that is separate from the DispatcherServlet application context.

Annotations

@Controller: Used to identify the processor class;

@RequestMapping: mapping rules for requesting to processor function methods;

@RequestParam: binding of request parameters to method parameters of handler function processing methods;

@ModelAttribute: binding of request parameters to command objects;

@SessionAttributes: Used to declare attributes stored at session level, placed on the processor class, usually listing the corresponding names of model attributes (such as @ ModelAttributes), then these attributes will be transparently saved to the session;

@InitBinder: Custom data binding registration support, used to convert request parameters to corresponding types of command object attributes;

RESTful architectural style support (via @PathVariable annotation and a few other features), and more annotation support has been introduced:

@CookieValue: Binding of cookie data to method parameters of processor function processing method;

@RequestHeader: Binding of header data to method parameters of processor function processing methods;

@RequestBody: binding of the requested body (type conversion via HttpMessageConverter);

@ResponseBody: The return value of the processor function processing method is used as the response body (type conversion through HttpMessageConverter);

@ResponseStatus: Defines the handler function processing method/status code and reason returned by exception handler;

@ExceptionHandler: annotationally-declared exception handler;

@PathVariable: binding of template variable parts in request URI to method parameters of processor function processing methods, thus supporting RESTful architecture-style URI;

POST Chinese garbled code solution

The spring Web MVC framework provides org.springframework.web.filter. CharacteristEncodingFilter to solve the Chinese garbled code problem caused by POST mode. The specific configuration is as follows:

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

CharacterEncodingFilter

/*

In the future, our project and all pages will be coded as UTF-8.

download address

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report