首发地址http://inmethetiger.iteye.com/blog/1679399
熟悉MultiActionController之后,就想写一个基于注解的MultiActionController处理多个用户请求。后来发现,基于注解的多请求处理,并不需要MultiActionController,而只是需要简单的在控制器中使用@Controller并在xxxx-servlet.xml中进行简单的配置就可以了。
所以,把代码帖一下,让自己记住。当然,这个只是最简单的实现。只是简单的使用了@RequestMapping,对于参数或者请求参数并没有涉及。主要原因是自己也是菜鸟。有的地方自己也无法解释清楚。 话不多说,代码如下:
-------------------------------------------------------------------------------------------------------------------------------------
@Controller
public class MyMutilActionController {
@RequestMapping(value = "/listing.do")
public ModelAndView listObject(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
mv.setViewName("list");
return mv;
}
@RequestMapping(value = "/delete.do")
public ModelAndView deleteObject(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
mv.setViewName("delete");
return mv;
}
@RequestMapping(value = "/update.do")
public ModelAndView updateObject(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
mv.setViewName("update");
return mv;
}
@RequestMapping(value = "/insert.do")
public ModelAndView insertObject(HttpServletRequest request,
HttpServletResponse response) {
ModelAndView mv = new ModelAndView();
mv.setViewName("insert");
return mv;
}
}
<?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: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.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="org.spring.controller"></context:component-scan> <!-- HandlerMapping --> <!-- 帮助DispatchServlet进行web请求的url到具体处理类的匹配,默认为BeanNameUrlHandleMapping --> <!-- <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> </bean> --> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> </bean> <!-- Controller --> <!-- 控制器,自己实现,收集参数,绑定参数到命令对象,调用业务对象,选择视图模型,返回视图,模型,数据 --> <!-- <bean name="/login.do" class="org.spring.controller.LoginController"></bean> --> <!-- ViewResover --> <!-- DispatcherServlet根据Controller中指定的逻辑视图查找相应的视图实现 --> <bean id="defaultViewResover" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
看xml文件可知,基于注解的Spring mvc基本上可以说是零配置。而且方便易懂,比如只要查看控制器就可以知道映射关系,而并不需要在xml文件中查看对应关系。