Handler

Posted 2226016500

tags:

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

1.Handler
实现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","今天学习spring mvc");
        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>

 可以访问:

 http://localhost:8080/01-springmvc-base/hello.do?actionname=doSecond

 http://localhost:8080/01-springmvc-base/hello.do?actionname=doFirst

如果不配置

 <property name="paramName" value="actionname"></property>
默认value为action

 


在访问多方法时:默认为InternalPathMethodNameResolver(属于AbstractUrlMethodNameResolver)

访问时:http://localhost:8080/01-springmvc-base/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/01-springmvc-base/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源代码深入解析

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