1.Handler
Implementation of handler second and third
1.1 Inheritance Abstractcontroller
Advantages: Can customize the request mode
PackageCn.happyl.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.AbstractController;ImportOrg.springframework.web.servlet.mvc.Controller;ImportOrg.springframework.web.servlet.support.WebContentGenerator; Public classMyabstractextendsAbstractcontroller {@OverrideprotectedModelandview handlerequestinternal (httpservletrequest request, httpservletresponse response)throwsException {modelandview mv=NewModelandview (); Mv.setviewname ("Index"); Mv.addobject ("MSG", "Learn Spring MVC today"); returnMV; }}
Application.xml
class= "Cn.happyl.controller.MyAbstract" > <property name= "Supportedmethods" value= "Post,get" ></property> </bean>
<!--trying to resolve the parser--
<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/web-inf/" ></property>
<property name= "suffix" value= ". JSP" ></property>
</bean>
<bean class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name= "Mappings" >
<props>
<prop key= "/hello.do" >first</prop>
</props>
</property>
</bean>
1.2 Inheritance Multiactioncontroller
Pros: You can have n methods in a controller
PackageCn.happyl.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.multiaction.MultiActionController; Public classMymultiactionextendsMultiactioncontroller { PublicModelandview Dofirst (httpservletrequest request,httpservletresponse response) {Modelandview MV=NewModelandview (); Mv.setviewname ("Index"); Mv.addobject ("MSG", "Execute first Method"); returnMV; } PublicModelandview Dosecond (httpservletrequest request,httpservletresponse response) {Modelandview MV=NewModelandview (); Mv.setviewname ("Index"); Mv.addobject ("MSG", "execution of the second method"); returnMV; } }
Application.xml
Method One: Parametermethodnameresolver
class= "Cn.happyl.controller.MyMultiAction" > <property name= "Methodnameresolver" ref= " Methodnameresolver "></property> </bean> class=" Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver ">
<property name= "paramname" value= "ActionName" ></property> </bean>
You can access:
Http://localhost:8080/01-springmvc-base/hello.do?actionname=doSecond
Http://localhost:8080/01-springmvc-base/hello.do?actionname=doFirst
If you do not configure
<property name= "paramname" value= "ActionName" ></property>
The default value is action
When accessing multiple methods: Default is Internalpathmethodnameresolver (belongs to Abstracturlmethodnameresolver)
When visiting:http://localhost:8080/01-springmvc-base/doSecond.do
Write the method name directly.
Method Two: Propertiesmethodnameresolver usage is as follows:
Configuring in Applicationcontext.xml
<bean class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name= "Mappings" >
<props>
<prop key= "/*.do" >first</prop>
</props>
</property>
</bean>
class= "Org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver" > < Property name= "Mappings" > <props> <prop key= "/first.do" >doFirst</prop> <prop key= "/second.do" >doSecond</prop> </props> </property></bean>
Can again be written in the browser's GET request: Http://localhost:8080/01-springmvc-base/first.do
Try the parser
<!--trying to parse 2 jstlview internal resource--
<bean id= "Internalresource" class= "Org.springframework.web.servlet.view.JstlView" >
<property name= "url" value= "/web-inf/index.jsp" ></property>
</bean>
<!--Redirectview External Resources--
<bean id= "JD" Class= "Org.springframework.web.servlet.view.RedirectView" >
<property name= "url" value= "http://www.jd.com" ></property>
</bean>
<bean class= "Org.springframework.web.servlet.view.BeanNameViewResolver"/>
The test classes are as follows:
PackageCn.happyl.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.web.servlet.ModelAndView;ImportOrg.springframework.web.servlet.mvc.multiaction.MultiActionController; Public classBookcontrollerextendsMultiactioncontroller { Publicmodelandview List (httpservletrequest request,httpservletresponse response) {Modelandview MV=NewModelandview (); Mv.addobject ("MSG", "Who am I?"); //The ID of the resource that needs to be accessed is established here, either an internal resource or an external resourceMv.setviewname ("JD"); returnMV; }}
Handler (b)