spring mvc 请求对应控制器的解析策略配置

一: mvc.xml 配置 

<?xml version="1.0" encoding="UTF-8"?>

<beans 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
<!-- MVC 配置 -->
<!-- 普通简单URL映射 -->
  <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
   <property name="urlMap">    
       <map>   
        <!-- 可注册多个控制器 --> 
           <entry key="/abc/*"><ref bean="mycontroller"/> </entry> 
           <!-- ... -->   
       </map>    
   </property>         
    </bean>  
<!-- 普通控制器1 :该方式实现的控制器需要继承MultiActionController 这个类  -->
<bean id="mycontroller" class="com.action.MyController">  
        <property name="methodNameResolver" ref="bResolver" /> <!-- 控制器关联解析策略 -->        
    </bean>
  
    <!-- 定义三种解析策略 -->  
    <bean id="aResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />  
    <bean id="bResolver" class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">  
         <property name="mappings">    
            <!-- 1 写法: -->   
            <!-- <value> /abc/test.tt=doTest</value>  -->
            
            <!-- 2 写法: -->
            <props>
            <prop key="/abc/test.tt">doTest</prop>
            <prop key="/abc/test1.tt">doTest1</prop>
            </props>
        </property>    
    </bean>  
    <bean id="cResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">  
        <!-- 1 根据请求参数func=doTest 对应请求方法 -->  
        <property name="paramName" value="func" />            
    </bean>  
          
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">  
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>  
        <property name="prefix" value="/WEB-INF/jsp/" />  
        <property name="suffix" value=".jsp" />  
    </bean>  
</beans>  

二:Control.java:
package com.action;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;    
import org.springframework.web.bind.ServletRequestDataBinder;  
import org.springframework.web.servlet.ModelAndView;  
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;  
 
public class MyController extends MultiActionController{  
     
   public ModelAndView to_add(HttpServletRequest request, HttpServletResponse response){  
       ModelAndView mav = new ModelAndView("add_emp");  
       mav.addObject("deptList", new String[]{"sales","manage"});  
       return mav;  
   }  
     
   public ModelAndView doTest(HttpServletRequest request, HttpServletResponse response){  
       System.out.println("doTest");  
       return null;  
   }  
   
   public ModelAndView doTest1(HttpServletRequest request, HttpServletResponse response){  
       System.out.println("doTest1");  
       return null;  
   }  
     
   public ModelAndView add_emp(HttpServletRequest request, HttpServletResponse response){  
       System.out.println("sdfsd");  
       return new ModelAndView("success");  
   }  
 
   @Override  
   protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {  
      //binder.registerCustomEditor(Date.class, new MyEditor());  
   }  

//还有利用注解的方式实现日期绑定:控制器继承该类
@InitBinder

protected void initBinder(WebDataBinder binder) {

binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));

}

  

     
}  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。