SpringMVC's Controller is used to handle user requests. Controllers are equivalent to Action in Struts1. Their implementation mechanism and operating principles are similar.
Controller is an interface, which generally directly inherits the AbstrcatController and implements the handleRequestInternal method. handleRequestInternal method is equivalent to Struts1's execute method
import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.AbstractController;public class CatController extends AbstractController{private ICatService catService;//setter and 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 data conversion code themselves
SpringMVC is unique in the processing of the view layer. handleRequestInternal returns a ModelAndView object, which can be regarded as encapsulation of JSP objects. ModelAndIView directly accepts the path to the JSP page. For example, the parameter "cat/listCat" is only part of the JSP path. The actual complete path is "WEB-INF/jsp/cat/catList.jsp". The part before and after the path is configured in the configuration file.
In addition to setting up JSP paths, ModelAndView can also directly pass Model objects to the View layer without putting them in the request in advance, such as newModelAndView("cat/listCat","cat", cat). If multiple parameters are passed, you can use Map, such as
Map map = newHashMap(); map.put("cat",cat); map.put("catList",catList); return new ModelAndView("cat/listCat",map);Generally, an independent xml file such as spring-action.xml is used to specifically 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"> <property name="prefix"> <value>/WEB-INF/jsp/</value><!-- JSP prefix--> </property> <property name="suffix"> <value>.jsp</value> <!-- JSP suffix--> </property> <!--Configuration URL Mapping--> <bean id="urlHandlerMapping"> <property name="mappings"> <props><!-Controller URL will be configured as "cat.mvc"--> <prop key="cat.mvc">catController</prop> <props> </property> </bean> <bean id="catController"> <property name="catService" ref="catService"></property> </bean> </beans> web.xml configuration <context-param><!-- Location of Spring 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><!-- Loading Spring configuration file using Listener--> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <servlet><!-- spring distributor--> <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><!-- Loading at startup--> </servlet> <servlet-mapping> <servlet-name> spring</servlet-name> <url>*.mvc</url> </servlet-mapping>
If a Controller wants to handle multiple business logic, you can use MultiActionController, which is equivalent to DispatchAction in Struts 1. It can distribute different requests to different methods according to a certain parameter.
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;public class CatController extends AbstractController{private ICatService catService;//setter and 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");}}Configure to spring-action.xml
<bean id="paraMethodResolver"> <property name="paramName"> <value>action</value><!-- Configure distribution parameters--> </property> <property name="defaultMethodName"> <value>list</value><!-- Configure default execution methods--> </property> </bean> <bean id="urlHandlerMapping"> <property name="mappings"> <props> <prop key="cat.mvc">catController</prop><!-- Accessing "cat.mvc"" will be handed over to catController for processing--> <prop key="catMulti.mvc">catMultiController</prop><!-- When accessing "catMulti.mvc", you will leave it to catMultiController for processing--> <props> </property> </bean> <bean id="catController"> <property name="catService" ref="catService"></property> </bean> <bean id="catMultiController"> <property name="catService" ref="catService"></property> </bean>
Summarize
The above is all the detailed explanation of the MVC module code in Spring, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out.