springMVC中处理器和适配器的使用,可以使用 配置 和 注解 方式
基于配置方式处理
配置处理器适配器,springMVC提供了 org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter 类实现,通过源码,可以了解到该适配器支持的 Handler 必须是 Controller接口的实现类,即需要实现Controller接口,SimpleControllerHandlerAdapter适配器才能支持执行。
- 处理器配置 (spring-mvc1.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!--基于配置形式来配置处理器映射器、处理器适配器和视图解析器-->
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--
配置 Handler实例,将实例交给容器管理
这里的 name和 id都可以获取到实例,但 id不能识别特殊字符,而name可以识别
-->
<bean name="/userlist1" class="com.tulun.controller.UserController1"/>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--SpringMVC 需要扫描注解-->
<context:component-scan base-package="com.tulun.controller"/>
<!--配置处理器映射器、处理器适配器-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--jsp页面前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--jsp页面后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 开发 Handler
/**
* 基于非注解实现Handler
* 必须实现Controller接口
*/
public class UserController1 implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//返回用户列表
ArrayList<User> users = new ArrayList<>();
User 嫦娥 = new User(1, "嫦娥");
User 后羿 = new User(2, "后羿");
User 猪八戒 = new User(3, "猪八戒");
users.add(嫦娥);
users.add(后羿);
users.add(猪八戒);
//返回 ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("users", users);
modelAndView.setViewName("userlist");
return modelAndView;
}
}
- 基于该配置形式时,一个Controller实现类只能针对特定的一个 url 做处理,即一个Handler只能一个类来处理,多个Handler(即针对不同的 url)是需要不同的Controller实现类来处理,并且要将实现类都 交给容器管理。
- 注意:在基于非注解的处理器中使用的两个框架类:
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
基于注解形式处理
- 在 spring-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!--SpringMVC需要扫描注解-->
<context:component-scan base-package="com.tulun.controller"/>
<!--配置处理器映射器、处理器适配器-->
<mvc:annotation-driven/>
<!-- <!–处理器映射器–>-->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.ResponseStatusExceptionResolver"/>-->
<!-- <!–处理器适配器–>-->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--jsp页面前缀-->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!--jsp页面后缀-->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 处理器映射器:
spring3.1之前使用 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
spring3.1之后使用 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 处理器适配器:
spring3.1之前使用 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
spring3.1之后使用 org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
- handler实现
@Controller
public class UserController {
/**
* 执行器
* @return
*/
@RequestMapping("/test")
public @ResponseBody String test(){
return "hello Si Chunsheng";
}
@RequestMapping("/userlist")
public ModelAndView userList(){
//返回用户列表
ArrayList <User> users = new ArrayList<>();
User ce = new User(1, "ce");
User hy = new User(2, "hy");
User zbj = new User(3, "zbj");
users.add(ce);
users.add(hy);
users.add(zbj);
//封装 ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
//封装数据
modelAndView.addObject("users",users);
//封装逻辑视图名
modelAndView.setViewName("userlist");
return modelAndView;
}
}