The controller of Spring MVC is used to process user requests. The controller is equivalent to the action in Struts 1, and their implementation mechanism and operating principle are similar
Controller is an interface, generally directly inherit Abstrcatcontroller, and implement the Handlerequestinternal method. The Handlerequestinternal method is equivalent to the Execute method of struts 1
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)) { returnthis.list (request,response); } } Protected Modelandview List (Httpservletrequestrequest,httpservletresponse response) throws exception{ list< cat> catlist =catservice.listcat (); Request.setattribute ("Catlist", catlist); Returnnew Modelandview ("Cat/listcat"); } }
Spring MVC does not encapsulate the built-in data, and developers can encapsulate their own data transformation code
The unique part of Spring MVC lies in the processing of the view layer. Handlerequestinternal returns the Modelandview object, which can be seen as encapsulation of the JSP object. Modelandiview directly accepts the path to the JSP page. For example, the parameter "Cat/listcat", just 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 developing a JSP path, the Modelandview can also pass the model object directly to the view layer without having to put it in the request beforehand, such as New Modelandview ("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);
Typically use a standalone XML file, such as Spring-action.xml, 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" class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name= "prefix" > <value>/WEB-INF/jsp/</value><!--JSP prefixes-- </property> <property name= "suffix" > <value>.jsp</value> <!--jsp suffix--</property> <!--configuration URL mapping--> <bean id= "Urlhand Lermapping "class=" org.springframework.web.servlet.handler.SimpleUrlHandleMapping "> <property name=" Mapp Ings "> <props><!-controller URL will be configured as" Cat.mvc "-<prop key= "Cat.mvc" >catController</prop> <props> </property> </bean> <bean id= "Catcontroller" class= "Com.clf.spring.CatController" > <property name= "c Atservice "ref=" Catservice "></property> </bean></beans> web. XML Configuration <context-param><!- -Spring configuration file location-<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 to load spring profiles--<listener-class> Org.sprin Gframework.web.context.ContextLoaderListener </listener-class></listener> <servlet><!-- Spring Distributor-<servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.ser Vlet. Dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</pa Ram-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& Lt;/servlet-name> <url>*.mvc</url></servlet-mapping>
If a controller is to handle multiple business logic, you can use Multiactioncontroller, which is equivalent to dispatchaction in struts 1, to distribute different requests to different methods depending 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{... Returnnew 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" class= " Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver "> <property name=" paramname " > <value>action</value><!--Configuring Distribution Parameters--</property> <property name= "def Aultmethodname "> <value>list</value><!--Configuration Default Execution Method--</property></bean> ; <bean id= "urlhandlermapping" class= "org.springframework.web.servlet.handler.SimpleUrlHandleMapping" > < ;p roperty name= "Mappings" > <props> <prop key= "Cat.mvc" >catcont roller</prop><!--Access "CAT.MVC" to Catcontroller process--<prop key= "Catmulti.mvc" >c atmulticontroller</prop><!--Access "CATMULTI.MVC" to catmulticontroller processing-<props> </property> </bean> <bean id= "Catcontroller" class= "Com.clf.spring.CatMultiCoNtroller "> <property name=" catservice "ref=" Catservice "></property> </bean> &L T;bean id= "Catmulticontroller" class= "Com.clf.spring.CatController" > <property name= "catservice" ref= "CA TService "></property> </bean>