Handler(二)

1.Handler
實現Handler第二種和第三種
1.1 繼承AbstractController
優勢:能定製請求方式java

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.xmlweb

 <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>spring

 

 

 

<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個方法mvc

 

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.xmlapp

方法一:ParameterMethodNameResolverjsp

 <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>

 能夠訪問:ide

 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;
    }

}
相關文章
相關標籤/搜索