目录
处理器映射器和处理器适配器的配置
SpringMVC中对组件的配置形式主要有两种,注解形式和非注解形式,下面来具体演示介绍这两种方式。
给定需求:展示用户列表
pojo类用户类(User):
public class User {
private Integer id;
private String name;
private String password;
public User(Integer id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
用户列表页面(userList.jsp):
<html>
<head>
<title>数据展示</title>
</head>
<body>
<table align="center" width="50%" border="3">
<thead>
<tr>
<td align="center">用户id</td>
<td align="center">用户名</td>
<td align="center">地址</td>
</tr>
</thead>
<tbody>
<%--c:forEach --%>
<c:forEach items="${users}" var="users">
<%--var集合--%>
<tr>
<td align="center">${users.id}</td>
<td align="center">${users.name}</td>
<td align="center">${users.password}</td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
</body>
</html>
1. 基于注解形式实现
组件的配置文件(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">
<!--扫描注解-->
<context:component-scan base-package="com.jing.controller"/>
<!--配置处理器映射器-->
<!--spring 3.1版本之前-->
<!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>-->
<!--spring 3.1版本之后-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<!--配置处理器适配器-->
<!--spring 3.1版本之前-->
<!--<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>-->
<!--spring 3.1版本之后-->
<!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
<!--springMVC中会加载框架默认的处理器映射器和处理器适配器-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
对于处理器映射器和处理器适配器等组件可以显性的声明类全路径,需要注意使用的spring版本,在spring3.1前后使用的类是不同的。
在spring3.1之前使用:
注解映射器:org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
注解适配器:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
在spring3.1之后使用:
注解映射器:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping
注解适配器:org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
另外:也可以直接通过<mvc:annotation-driven/>读取框架提供的默认的组件。
编写Handler层(UserController.java):
@Controller
public class UserController {
@RequestMapping("/userlist")
public ModelAndView userList() {
ArrayList<User> users = new ArrayList <>();
users.add(new User(1,"张三","西安"));
users.add(new User(2,"李四","上海"));
users.add(new User(3,"王麻子","北京"));
ModelAndView modelAndView = new ModelAndView();
//数据填充
modelAndView.addObject("users",users);//对应前端var
//指定路径 全路径
modelAndView.setViewName("userlist");
return modelAndView;
}
@RequestMapping("/usertest")
@ResponseBody //以JSON数据返回
public String test() {
return "Hello";
}
}
注解使用:
@Controller注解:将类交给容器管理;
@RequestMapping:给定URL,通过URL找到具体处理业务逻辑。
2. 基于非注解形式实现
在各组件的配置文件中,分析映射器和适配器。
组件的配置文件(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">
<!--URL-->
<bean name="/user1list" class="com.jing.controller.User1Controller"/>
<!--配置处理器映射器-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--配置处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!--视图解析器-->
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
处理器适配器类是SimpleControllerHandlerAdapter,通过源码可知,supports是判断当前适配器能不能支持这个handler,要能够被处理器适配器识别的类必须是Controller接口的具体实现类。
编写Handler层(User1Controller.java):(要求编写的Handler实现Controller
接口)
*
*基于非注解形式的处理器要能被处理器适配器识别
*必须实现org.springframework.web.servlet.mvc.Controller接口
*该接口中的handleRequest需要实现
**/
public class User1Controller implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
ArrayList<User> users = new ArrayList<>();
users.add(new User(1, "张三", "西安"));
users.add(new User(2, "李四", "上海"));
users.add(new User(3, "王麻子", "北京"));
ModelAndView modelAndView = new ModelAndView();
//数据填充
modelAndView.addObject("users", users);//对应前端var
//指定路径 全路径
modelAndView.setViewName("userList");
return modelAndView;
}
}
以上都是学习过程中的总结,如果有错误或者有疑问,欢迎一起交流吖~~