Detail MVC module code in Spring

  • 2020-12-05 17:12:40
  • OfStack

Controller of SpringMVC is used to handle user requests. Controller is equivalent to Action in Struts1, and their implementation mechanism and operation principle are similar

Controller is an interface that generally inherits AbstrcatController directly and implements handleRequestInternal methods. The handleRequestInternal method is equivalent to the execute method of Struts1


import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class CatController extends AbstractController{
	private ICatService catService;
	//setter , getter slightly  
	protected ModelAndView handleRequestInternal(HttpServletRequestrequest,HttpServletResponse response) throws Exception{
		String action =request.getParameter("action");
		if("list".equals(action)){
			return this.list(request,response);
		}
	}
	protected ModelAndView list(HttpServletRequestrequest,HttpServletResponse response) throws Exception{
		List<Cat> catList =catService.listCat();
		request.setAttribute("catList", catList);
		return new ModelAndView("cat/listCat");
	}
}

SpringMVC does not have built-in data encapsulation; developers can encapsulate their own data conversion code

What makes SpringMVC unique is the treatment of the view layer. handleRequestInternal returns an ModelAndView object, which can be thought of as a wrapper around an JSP object. ModelAndIView accepts the path of the JSP page directly. Parameter "cat/listCat", for example, is just 1 part of JSP path, actual full path is "WEB - INF/jsp/cat/catList jsp", the path before and after the part is configured in the configuration file

In addition to specifying the JSP path, ModelAndView can also pass Model objects directly to the View layer without putting them in request, for example, newModelAndView(" cat/listCat ", "cat",cat), if passing multiple parameters, Map can be used, for example


Map map = newHashMap(); 
map.put("cat",cat); 
map.put("catList",catList); 
return new ModelAndView("cat/listCat",map); 

Use a separate xml file such as ES53en-ES54en. xml to configure web-related components


<?xml version= "1.0" encoding="UTF-8"?> 
<!DCTYPEbeans PUBLIC "-//SPRING//DTD BEAN//EN" 
 "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
       <property name="prefix"> 
          <value>/WEB-INF/jsp/</value><!-- JSP The prefix --> 
       </property> 
       <property name="suffix"> 
          <value>.jsp</value>         <!-- JSP The suffix --> 
       </property> 
  
   <!--  configuration URL Mapping--> 
   <bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandleMapping"> 
       <property name="mappings"> 
          <props><! - Controller the URL Will be configured to "cat.mvc"--> 
              <prop key="cat.mvc">catController</prop> 
          <props> 
       </property> 
   </bean> 
   <bean id="catController" class="com.clf.spring.CatController"> 
       <property name="catService" ref="catService"></property> 
   </bean> 
</beans> 
  
web.xml configuration  
<context-param><!-- Spring The location of the configuration file --> 
   <param-name>contextConfigLocation</param-name> 
   <param-value> 
       /WEB-INF/classes/applicationContext.xml, 
       /WEB-INF/classes/spring-action.xml   
   </param-value> 
</context-param> 
  
<listener><!--  use Listener loading Spring The configuration file --> 
   <listener-class> 
       org.springframework.web.context.ContextLoaderListener 
   </listener-class> 
</listener> 
  
<servlet><!-- spring The dispenser --> 
   <servlet-name>spring</servlet-name> 
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
   <init-param> 
       <param-name>contextConfigLocation</param-name> 
       <param-value>/WEB-INF/classes/spring-action.xml</param-value> 
   </init-param> 
   <load-on-startup>1</load-on-startup><!--  Startup load --> 
</servlet> 
  
<servlet-mapping> 
   <servlet-name> spring</servlet-name> 
   <url>*.mvc</url> 
</servlet-mapping> 

If a single Controller handles more than one business logic, you can use MultiActionController, equivalent to DispatchAction in Struts 1, to distribute different requests to different methods based on a parameter


import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class CatController extends AbstractController{
	private ICatService catService;
	//setter , getter slightly  
	protected ModelAndView add(HttpServletRequestrequest,HttpServletResponse response) throws Exception{
		 ...  
		       return new ModelAndView( " cat/addCat " );
	}
	protected ModelAndView list(HttpServletRequestrequest,HttpServletResponse response) throws Exception{
		List<Cat> catList =catService.listCat();
		request.setAttribute( " catList " , catList);
		return new ModelAndView( " cat/listCat " );
	}
}

Configuration to spring - action xml


<bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> 
   <property name="paramName"> 
       <value>action</value><!--  Configure distribution parameters --> 
   </property> 
   <property name="defaultMethodName"> 
       <value>list</value><!--  Configure the default execution method --> 
   </property> 
</bean> 
  
<bean id="urlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandleMapping"> 
       <property name="mappings"> 
          <props> 
              <prop key="cat.mvc">catController</prop><!--  access "cat.mvc" Is to catController To deal with --> 
              <prop key="catMulti.mvc">catMultiController</prop><!--  access "catMulti.mvc" Is to catMultiController To deal with --> 
          <props> 
       </property> 
   </bean> 
  
   <bean id="catController" class="com.clf.spring.CatMultiController"> 
       <property name="catService" ref="catService"></property> 
   </bean> 
  
   <bean id="catMultiController" class="com.clf.spring.CatController"> 
       <property name="catService" ref="catService"></property> 
   </bean> 

conclusion

The above is all about the MVC module code detail in Spring, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out.


Related articles: