使用CoC,惯例优先原则(convention over configuration)的方式来配置SpringMVC可以帮我们声明Controller的时候省下很多功夫。 只要我们的Controller是以XXXController的方式命名的话就可以使用到CoC带来的好处。 例如,对于普通的Controller(非MultiActionController),CoC帮我们做以下的映射: HomeController映射到/home*请求URL DisplayShoppingCartController映射到/displayshoppingcart*请求URL
例如,http://106.120.***.**:8080/MallManager/forms/mallorgan/goToPage?goToPage=mallorganconfig
提交到MallorganController.java类的goToPage方法,传的参数是goToPage,值是mallorganconfig
@Controller
public class MemberController {
@RequestMapping
public ModelAndView goToPage(@RequestParam String goToPage,
@RequestParam(defaultValue = "0") String memberid, String oprFlag)
throws Exception {
if (goToPage.equals("memberList")) {}
}
}
注意:应该映射成使用全部小写的请求URL 对与MultiActionController,映射规则如下: 例如,我有一个UserInfoController,里面有一个方法valid,那么就会映射成/userinfo/valid.do(.do对应你设置的dispatcher-servlet的url-pattern)。 使用CoC带来的好处就是我们不需要再忍受必须先定义它们,然后还要维护冗长的SimpleUrlHandlerMapping的枯燥。 规则就这么多,以下是配置。 1.dispatcher-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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- ControllerClassNameHandlerMapping用于匹配我们的Controller --> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /> <!-- 使用annocation创建Controller的bean --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- Controller的包,使容器启动的时候可以找的并实例化Controller --> <context:component-scan base-package="test.controller"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> </beans> 2.编写我们的Controller: package test.controller; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; @Controller public class MYController extends MultiActionController { @RequestMapping public void test(HttpServletRequest request, HttpServletResponse response){ PrintWriter out = null; try { out = response.getWriter(); out.println("ok"); }catch(Exception e) { out.println("not ok"); }finally { out.close(); } } } 这里我们可以看到,我们只需要在class前面声明@Controller,方法前面声明@RequestMapping就可以了,其他什么都不需要自己配置,最后使用地址/my/test.do就可以调用了。 另外,如果你想对这些使用ControllerClassNameHandlerMapping匹配的Controller使用拦截器的话,只需要为ControllerClassNameHandlerMapping bean的interceptors list注入拦截器就可以了: <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> <property name="interceptors"> <list> <ref bean="myInter" /> </list> </property> </bean> <bean id="myInter" class="test.MyInter" /> 如果我配置拦截器 但是不想让他拦截所有的请求 应该怎么配置? 那你就用不到ControllerClassNameHandlerMapping了,换SimpleUrlHandlerMapping,手动配置。 配置例子如下: <bean id="urlMapping1" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="inter1" /> </list> </property> <property name="mappings"> <props> <prop key="/MyController1.do">myController1</prop> </props> </property> </bean> <bean id="myController2" class="controller.MyController2"></bean> <bean id="urlMapping2" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="interceptors"> <list> <ref bean="inter2" /> </list> </property> <property name="mappings"> <props> <prop key="/MyController2.do">myController2</prop> </props> </property> </bean> <bean id="inter1" class="interceptor.Inter1" /> <bean id="inter2" class="interceptor.Inter2" /> <bean id="myController1" class="controller.MyController1"></bean> <bean id="myController2" class="controller.MyController2"></bean> Inter1只拦截/MyController1.do,Inter2只拦截/MyController2.do