Handler

Posted 晨曦-荒微凉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Handler相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

自定义无内存泄漏的Handler内部类

什么时候可以在android中使用强引用,这个代码是否泄漏?

不频繁的 Handler.post 导致大量丢帧和崩溃

什么是生命周期感知方式来实现重复的AsyncTask?

Android之Handler源代码深入解析

无限循环 - 延迟 - 单独的线程