目录
1、处理器和映射器的配置
SpringMVC中对组件的配置形式主要有两种,注解形式和非注解形式
给定需求:展示用户列表
1.1、用户类:
public class User {
private Integer id;
private String name;
private String address;
//省略
}
1.2、用户列表页面:jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>数据展示</title>
</head>
<body>
<h1 align="center">${class} </h1>
<table align="center" border="1">
<thead>
<tr>
<td>用户id</td>
<td>用户名</td>
<td>地址</td>
</tr>
</thead>
<tbody>
<%--c:forEach --%>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.address}</td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
</body>
</html>
2、基于注解形式实现
2.1、组件的配置文件,scources目录下的配置文件
<?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">
<!--配置处理器映射器-->
<!--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">
</bean>
</beans>
对于处理器映射器和处理器适配器等组件可以显性的声明类全路径,需要主要使用的spring版本,在spring3.1前后使用的类是 不同的。
也可以直接通过<mvc:annotation-driven/>读取框架提供的默认的组件
2.2、基于注解的Handler
@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,"小李","北京"));
users.add(new User(4,"小张","陕西西安"));
ModelAndView modelAndView = new ModelAndView();
//数据填充
modelAndView.addObject("users",users);
//指定路径 全路径
modelAndView.setViewName("userlist");
return modelAndView;
}
@RequestMapping("/usertest")
@ResponseBody //以JSON数据返回
public String test() {
return "Hello Tulun Java212";
}
}
注解使用:@Controller注解 将类交给容器管理
@RequestMapping 给定URL,。通过URL找到具体处理业务逻辑
3、基于非注解形式实现
3.1、在各组件的配置文件中,分析处理器适配器
<?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"/>
<!--配置处理器适配器-->
<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,通过源码可知,要能够被处理器适配器识别的类必须是Controller接口的具体实现类
3.2、基于非注解形式的handler
/**
* 基于非注解形式的处理器要能被处理器适配器识别
* 必须实现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,"小李","北京"));
users.add(new User(4,"小张","陕西西安"));
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("users",users);
modelAndView.setViewName("userList");
System.out.println("测试请求");
return modelAndView;
}
}