Details of spring MVC configuration file

HandlerMapping processor mapping

After the HTTP request is intercepted by the dispatcher servlet, it will call HandlerMapping to process. HandlerMapping determines which controller to call to process according to the mapping relationship between the URL < = > controllers.


There are two types of HandlerMapping:

  • BeanNameUrlHandlerMapping

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<bean name="/userController" class="com.chy.controller.UserController" />

Bean name url, as the name implies, the mapping relationship is beanname < = > url, the url address corresponds to the bean name, and the controller to be called is determined according to the beanname in the url.



  • SimpleUrlHandlerMapping

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <!-- Use props To configure url-controller Mapping between -->
                <!-- One prop Configure a map, key Appoint url,value Appoint bean Of id or name. Multiple can be used prop.  -->
                <prop key="/userController">userController</prop>
    <bean name="userController" class="com.chy.controller.UserController" />

The url is / userController. Go to < props > to find key=/userController's < prop > to get the name or id of the controller to be called.





HandlerAdapter processor adapter

After the HandlerMapping determines the controller to be called, it tells the dispatcher servlet the controller to be called,

The dispatcher servlet calls the HandlerAdapter to which the controller belongs, and then the HandlerAdapter calls the controller.


There are two types of handleradapters:

  • SimpleControllerHandlerAdapter (recommended)

    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
public class UserController implements Controller {
    public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest, javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
        //The view can be specified in the constructor, or the empty parameter constructor can be used first setter Method setting
        ModelAndView mav = new ModelAndView("user_info");
        User user = new User();
        //Passing data to a view
        mav.addObject("user", user);
        return mav;

SimpleControllerHandlerAdapter, with Controller in its name, naturally implements the Controller interface.

Method to return a value of type ModelAndView.



The ModelAndView is returned to the HandlerAdapter by the controller, and then to the dispatcher servlet by the HandlerAdapter.

The DispatcherServlet calls the configured ViewResolver to parse the ModelAndView, obtain the model (data) and view (view name), and return the data and view name to the DispatcherServlet.

Dispatcherservert renders the View (fills the model data into the View), and returns the rendered View to a browser.



So you need to use it with ViewResolver:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />

There are several view parsers, which are not covered here.




  • HttpRequestHandlerAdapter

<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
public class UserController implements HttpRequestHandler{
    public void handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        User user = new User();

        //Passing data to a view

        //Forward request to view (address bar's url Invariable)
        RequestDispatcher dispatcher = httpServletRequest.getRequestDispatcher("/WEB-INF/jsp/user_info.jsp");

The adapter corresponds to the implemented interface. Using the request forwarding implementation, the return value is void.

ViewResolver is used to parse ModelAndView. Since the return value is void, the view resolver is not used. There is no splicing of view names, and the full path of the view needs to be written.

This is the implementation of Servlet, not recommended. Try to use the mode of ModelAndView, which is more in line with the idea of mvc.

Tags: Java JSP

Posted on Wed, 29 Jan 2020 06:53:49 -0800 by Afser