AbstractController
先创建一个MyAbstorctController类,并且让他继承AbstractController父类,并实现该父类中的方法.
public class MyAbstorctController extends AbstractController{ protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv=new ModelAndView(); mv.setViewName("index"); return mv; } }
abstorctcontroller.xml配置
<!--处理器映射器--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/hello.do" value="fistController"></entry> </map> </property> </bean> <!--注册处理器--> <!--<bean id="/hello.do" class="cn.happy.controller.Onecontriller"></bean>--> <bean id="fistController" class="cn.happy.controller.MyAbstorctController"> <property name="supportedMethods"> <set> <value>POST</value> <value>GET</value> </set> </property> </bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean>
MultiActionController类的实现方式有三种:
首先创建MyMutiActionController类并继承MultiActionController类,自定义方法名
public class MyMutiActionController extends MultiActionController { public ModelAndView insert(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv=new ModelAndView(); mv.setViewName("insert"); return mv; } public ModelAndView delete(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv=new ModelAndView(); mv.setViewName("delete"); return mv; } }(一)multiactioncontroller.xml配置,默认的方法解析器<!--处理器映射器--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/*.do" value="mutiactioncontroller"></entry> </map> </property> </bean> <!--注册处理器--> <bean id="mutiactioncontroller" class="cn.happy.controller.MyMutiActionController"> </bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean>(二)propertiesMrthodNameresolver.xml配置<!--处理器映射器--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/*.do" value="mutiactioncontroller"></entry> </map> </property> </bean> <!--属性方法名称解析器--> <bean id="propertyNameResoler" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/first.do">insert</prop> <prop key="/second.do">delete</prop> </props> </property> </bean> <!--注册处理器--> <bean id="mutiactioncontroller" class="cn.happy.controller.MyMutiActionController"> <property name="methodNameResolver" ref="propertyNameResoler"></property> </bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean>(三)propeterMrthodNameresolver.xml配置,参数方法解析器<!--处理器映射器--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/hello.do" value="mutiactioncontroller"></entry> </map> </property> </bean> <!--属性方法名称解析器--> <bean id="properterNameResoler" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName" value="actionName"></property> </bean> <!--注册处理器--> <bean id="mutiactioncontroller" class="cn.happy.controller.MyMutiActionController"> <property name="methodNameResolver" ref="properterNameResoler"></property> </bean> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"></property> <property name="suffix" value=".jsp"></property> </bean>视图解析器:先创建一个MyViewResoler类,并继承MultiActionController类,自定义两个方法名。public class MyViewResoler extends MultiActionController { public ModelAndView insert(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv=new ModelAndView(); mv.setViewName("innserView"); return mv; } public ModelAndView inserts(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv=new ModelAndView(); mv.setViewName("jd"); return mv;} }创建ViewResoler.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--处理器映射器--> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/hello.do" value="mutiactioncontroller"></entry> </map> </property> </bean> <!--属性方法名称解析器--> <bean id="properterNameResoler" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <!-- <property name="paramName" value="actionName"></property>--> </bean> <bean id="propertyNameResoler" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"> <property name="mappings"> <props> <prop key="/first.do">insert</prop> <prop key="/second.do">delete</prop> </props> </property> </bean> <!--内部视图对象--> <bean id="innserView" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/WEB-INF/index.jsp"></property> </bean> <!--外部视图对象--> <bean id="jd" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="https://www.jd.com7"></property> </bean> <!--BeanName视图解析器--> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <!--注册处理器--> <bean id="mutiactioncontroller" class="cn.happy.controller.MyViewResoler"> <property name="methodNameResolver" ref="properterNameResoler"></property> </bean> </beans>