目录
以实现业务逻辑(展现用户列表)来说明这两种方法
先给定User类
public class User {
private Integer id;
private String name;
private String address;
public User(Integer id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", address='" + address + '\'' +
'}';
}
}
展现用户类userlist.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>
一、注解形式
1、各组件的配置文件 springmvc1.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">
<!-- 注意在注解形式中一定要开启扫描注解,不然会找不到路径404错误 -->
<context:component-scan base-package="com.che.controller"></context:component-scan>
<!--配置处理器映射器-->
<!--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">
<!--指定了前后缀,在输入url时只需要输入类名即可-->
<property name="prefix" value="WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
注意:!!!
对于处理器映射器和处理器适配器等组件可以显性的声明类全路径,需要主要使用的spring版本,在spring3.1前后使用的类是 不同的
也可以直接通过<mvc:annotation-driven/>读取框架提供的默认的组件
2、Controller类
//记得要在类上加上注解
@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";
}
}
运行结果
注解使用:
@Controller注解 将类交给容器管理
@RequestMapping 给定URL,通过URL找到具体处理业务逻辑
二、非注解形式实现
1、各组件的配置文件 springmvc2.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"/>
<!--配置处理器适配器-->
<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接口的具体实现类
2、User1Controller类
必须实现Controller类,重写handleRequest方法,才能被处理器适配器识别
/**
* 基于非注解形式的处理器要能被处理器适配器识别
* 必须实现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;
}
}
运行结果
总结:
通常一般情况下使用注解形式
使用注解形式需要注意 配置注解 和注解扫描是否开启
使用非注解形式就需要在controller类上实现Controller类,重写handleRequest方法