码迷,mamicode.com
首页 > 其他好文 > 详细

Handler

时间:2016-12-25 18:23:58      阅读:194      评论:0      收藏:0      [点我收藏+]

标签: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;
    }

}
技术分享

 

Handler

标签:rect   mvc   throws   intern   mode   ati   默认   cal   index.jsp   

原文地址:http://www.cnblogs.com/baixingqiang/p/6219887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!