Annotated and non annotated processor mapper and processor adapter in srpingmvc.xml

Catalog

1. non annotations

1.1 non annotated processor adapter

1.2 non annotated processor mapper

1.2.1 BeanNameUrlHandlerMapping

1.2.2 SimpleUrlHandlerMapping

1.3 summary

2. notes

1. non annotations

1.1 non annotated processor adapter

<! -- non annotated processor adapter -- >
<! -- the Handler written is required to implement the Controller interface -- >
  <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
		
<! -- another non annotated adapter -- >
<! -- the Handler written is required to implement HttpRequestHandler interface -- >
  <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>

Both require the implementation of HandlerAdapter interface. We can observe the source code of SimpleControllerHandlerAdapter

It can be found that it implements the HandlerAdapter interface, and we can determine that the handler we write to implement the controller interface according to the value it returns. The Handler example is as follows.

//Processor implementing controller interface

public class ItemController1 implements Controller {

	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		
		//Call service to find the database and the list of goods. Here, static data simulation is used
		List<Items> itemsList = new ArrayList<Items>();
		
		//Add static data to the list
		Items item_1 = new Items();
		item_1.setName("Mi phones");
		item_1.setPrice(1600f);
		item_1.setDetail("Xiaomi 8 Youth Edition");
		
		Items item_2 = new Items();
		item_2.setName("HUAWEI mobile phone");
		item_2.setPrice(1700f);
		item_2.setDetail("HUAWEI P30");
		
		itemsList.add(item_1);
		itemsList.add(item_2);
		
		//Return to ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		//Equivalent to setAttribute of request, add data through itemsList in jsp page
		modelAndView.addObject("itemsList", itemsList);
		
		//Specified view
		modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
		return modelAndView;
	}

}

Let's continue to look at the source code of HttpRequestHandlerAdapter

The HandlerAdapter interface is also implemented. According to its return value, we can also know that the handler to be written should implement the HttpRequestHandler interface. Here I will put a handler instance for your reference

//Processor implementing HttpRequestHandler interface

public class ItemController2 implements HttpRequestHandler {

	@Override
	public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//Call service to find the database and the list of goods. Here, static data simulation is used
		List<Items> itemsList = new ArrayList<Items>();
		
		//Add static data to the list
		Items item_1 = new Items();
		item_1.setName("Mi phones");
		item_1.setPrice(1600f);
		item_1.setDetail("Xiaomi 8 Youth Edition");
		
		Items item_2 = new Items();
		item_2.setName("HUAWEI mobile phone");
		item_2.setPrice(1700f);
		item_2.setDetail("HUAWEI P30");
		
		itemsList.add(item_1);
		itemsList.add(item_2);
		//Set model data
		request.setAttribute("itemsList", itemsList);
		//Set forwarding view
		request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);
		//Using this method, you can set the data format of the response by modifying the response, such as the response json data.
        //response.setCharacterEncoding("utf-8");
        //response.setContentType("application/json;charset=utf-8");
        //response.getWriter().write("json string");
	}

}

1.2 non annotated processor mapper

There are two cases of non annotated processor mapper, one is BeanNameUrlHandlerMapping, the other is SimpleUrlHandlerMapping. Now let's start with these two kinds of analysis.

1.2.1 BeanNameUrlHandlerMapping

Configure a Handler

<!-- To configure Handler -->
  <bean id="itemController1" name="/queryItems_1.action" class="cn.itcast.ssm.controller.ItemController1"/> 

Then we continue to configure the non annotated processor mapper

<! -- the non annotated processor mapper looks up the bean's name as the url, and needs to specify the beanname (that is, the url) when configuring the Handler 
	All mappers implement the HandlerMapping interface. >
  <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

It can be seen that what this mapper needs is to configure the url address accessed on the name when configuring the Handler, so that a non annotated processor mapper can be configured.

1.2.2 SimpleUrlHandlerMapping

The first step is to configure the Handler first

<!-- To configure Handler -->
<bean id="itemController2" class="cn.itcast.ssm.controller.ItemController2"/>

Continue to configure mapper

 <!--simple url mapping  -->
 <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="mappings">
		<props>
			<!-- The value is bean Of id ,key It's a visit url address -->
		    <prop key="/queryItems1.action">itemController2</prop>
			<prop key="/queryItems2.action">itemController2</prop>
		</props>
	</property>
</bean>

From the configuration, we can see that it is not important to configure the name of the Handler at this time. You need to configure its id. from this, you can see that a controller can map to multiple URLs.

1.3 summary

Multiple mappers can coexist. The front-end controller determines which mapper the URL can let map, and then the correct mapper can handle it. And the discovery processor mapper is: find the Handler according to our URL. The processor adapter is to execute the Handler according to the rules it requires. There is only one method in a class corresponding to the non annotated HandlerMapping and HandlerAdapter, which has limited processing power and is cumbersome.

2. notes

The annotated processor adapter and processor mapper are applicable in development. And the configuration is also relatively simple. First of all, it depends on the configuration of the Handler.

<! -- the Handler of annotation can be configured individually
 Using scan component in actual development
 -->
 <!-- bean class="cn.itcast.ssm.controller.ItemController3" -->
 
 <! -- scan Controller,service 
 Let the component scan the Controller and specify the Controller package
 -->
 <context:component-scan base-package="cn.itcast.ssm.controller">
 </context:component-scan>

It can be configured individually. You can see the attribute configuration with less id and name, but it is cumbersome. Generally, component scanning is used.

Continue with processor mapper and processor adapter configuration

<! -- Annotation mapper -- >
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<! -- Annotation adapter -- >
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

<! -- use MVC: annotation driven to replace the above configuration of annotation mapper and annotation adapter
 MVC: annotation driven loads many parameter binding methods by default, such as json transformation parser.
This method is used in actual development
  -->
<mvc:annotation-driven></mvc:annotation-driven>

It can be seen that there is also a general method to configure < MVC: annotation driven > < MVC: annotation driven > in actual development. Is it a lot easier to annotate the code. Let's take a look at the writing of the controller.

//Annotated controller
@Controller
public class ItemController3 {

	//Mapper for annotations
	@RequestMapping("/queryItems")
	public ModelAndView queryItems() throws Exception{
		
		
		//Call service to find the database and the list of goods. Here, static data simulation is used
		List<Items> itemsList = new ArrayList<Items>();
		
		//Add static data to the list
		Items item_1 = new Items();
		item_1.setName("Mi phones");
		item_1.setPrice(1600f);
		item_1.setDetail("Xiaomi 8 Youth Edition");
		
		Items item_2 = new Items();
		item_2.setName("HUAWEI mobile phone");
		item_2.setPrice(1700f);
		item_2.setDetail("HUAWEI P30");
		
		itemsList.add(item_1);
		itemsList.add(item_2);
		
		//Return to ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		//Equivalent to setAttribute of request, add data through itemsList in jsp page
		modelAndView.addObject("itemsList", itemsList);
		
		//Specified view
		//modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
		//If the above jsp path is configured in the spring.xml file, it can be changed to the following code
		modelAndView.setViewName("items/itemsList");
		
		return modelAndView;
		
	}


}

When writing Controller, we need to use two annotations: one is that @ Controller indicates that it is a Controller, and the other is that @ RequestMapping specifies the url to access, so we can write multiple methods in a class.

Published 10 original articles, won praise 3, visited 1563
Private letter follow

Tags: JSP JSON Database Mobile

Posted on Sat, 01 Feb 2020 04:36:35 -0800 by mazazino