标签:rect mvc throws intern mode ati 默认 cal index.jsp
1.1 继承AbstractController
优点:能定制请求方式
package cn.happyl.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; import org.springframework.web.servlet.mvc.Controller; import org.springframework.web.servlet.support.WebContentGenerator; public class MyAbstract extends AbstractController { @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv =new ModelAndView(); mv.setViewName("index"); mv.addObject("msg","今天是圣诞节"); return mv; } }
application.xml
<bean id="first" class="cn.happyl.controller.MyAbstract"> <property name="supportedMethods" value="POST,GET"></property> </bean>
<!--试图解析器 -->
<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 继承MultiActionController
优点:可以在一个Controller中有N个方法
package cn.happyl.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class MyMultiAction extends MultiActionController { public ModelAndView doFirst(HttpServletRequest request,HttpServletResponse response){ ModelAndView mv =new ModelAndView(); mv.setViewName("index"); mv.addObject("msg", "一"); return mv; } public ModelAndView doSecond(HttpServletRequest request,HttpServletResponse response){ ModelAndView mv =new ModelAndView(); mv.setViewName("index"); mv.addObject("msg", "二"); return mv; } }
application.xml
方法一:ParameterMethodNameResolver
<bean id="first" class="cn.happyl.controller.MyMultiAction"> <property name="methodNameResolver" ref="methodNameResolver"></property> </bean> <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="actionname"></property> </bean>
启动Tomcat 访问
http://localhost:8080/springmvc/hello.do?actionname=doSecond
http://localhost:8080/springmvc/hello.do?actionname=doFirst
如果不配置
<property name="paramName" value="actionname"></property>
默认value为action
在访问多个方法时:默认为InternalPathMethodNameResolver(属于AbstractUrlMethodNameResolver)
访问时:http://localhost:8080/springmvc/doSecond.do
直接写方法名.do即可
方法二:propertiesmethodnameresolver 用法如下:
在applicationContext.xml中配置
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/*.do">first</prop>
</props>
</property>
</bean>
<bean id="methodNameResolver" 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>
可以再浏览器的get请求中写:http://localhost:8080/springmvc/first.do
试图解析器
<!--试图解析器 2 jstlview 内部资源-->
<bean id="internalResource" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/index.jsp"></property>
</bean>
<!--redirectView外部资源-->
<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"/>
测试类如下:
package cn.happyl.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class BookController extends MultiActionController { public ModelAndView list(HttpServletRequest request,HttpServletResponse response){ ModelAndView mv =new ModelAndView(); mv.addObject("msg","今天是圣诞节"); //这里制定需要访问的资源的id,可以是内部资源也可以是外部资源 mv.setViewName("jd"); return mv; } }
标签:rect mvc throws intern mode ati 默认 cal index.jsp
原文地址:http://www.cnblogs.com/baixingqiang/p/6219887.html