SpringMVC 控制器写多个方法(非注解方式)

Controller类有两种方法

1,implements Controller(实现Controller接口)

2,extends MultiActionController(继承 MultiActionController)

但是实现接口有缺陷,只能写一种方法(handleRequest),比如完成某一业务对象的增,删,改,查,都属于一类相关的业务。把一类相关的操作分布到不同的类去完成,违返“高内聚”的设计原则。所以一般都会采用第二种方法。

 

springmvc-servlet.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:context="http://www.springframework.org/schema/context" 
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 
    <mvc:annotation-driven />
    <context:component-scan base-package="com" /> 
    <bean id="irViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/page/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    <!--  MultiAction -->
    <!-- 引入多视图控制 -->
    <bean id="paramMethodResolver"
        class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName" value="method">
        </property>
    </bean>
    <bean id="simpleUrlHandlerMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/testinterceptor">testInterceptorController</prop>
            </props>
        </property>
    </bean>
    <!-- 将多视图控制paramMethodResolver注入到自己的控制器--> 
    <bean id="testInterceptorController" class="com.TestInterceptorController">
        <property name="methodNameResolver" ref="paramMethodResolver"></property>
    </bean>
    
</beans>

控制器:

package com;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@SuppressWarnings("deprecation")
public class TestInterceptorController extends MultiActionController {

    public String show(HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("date", new Date());
        return "show";
    }
}

*这里要用request进行传值

输入访问:......?method=show

posted @ 2017-08-22 17:40  Junsept  阅读(553)  评论(0编辑  收藏  举报