深入分析springmvc中的处理器映射器和适配器
前言
随着springmvc的注解功能使用,springmvc已经逐渐取代struts2成为目前比较主流的mvc框架,在springmvc中除了核心控制器DispatcherServlet这个核心组件之外,还有在springmvc中的处理器映射器和适配器,这两个组件在springmvc整个的运行流程之中扮演一个很重要的角色,至于映射器和适配器这两个组件他们的功能,我们可以这样理解,映射器主要是跟我们在浏览器上输入的url来映射对应的Handle,具体的映射规则需要根据使用哪一个映射器来决定,而适配器主要是决定调用哪个Handler来实现具体的业务逻辑,随着注解的发展,映射器和适配器的使用也越来越方便,但是熟悉这两个组件的底层实现,对我们在日常开发和对springmvc的理解也是有帮助的,下面我们一起来看看非注解的映射器和适配器具体实现案例。
1.目录
2.jar包:
3.web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置springmvc前端控制器 DispatcherServlet-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等)
若不配置,默认加载WEB-INF/servlet名称-servlet(springmvc-servlet.xml) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4. 配置文件springmvc.xml:(加载控制器ItemsController1)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 配置Handler , 指定beanname(就是url) -->
<bean id="itemsController1" name="/queryItems.action" class="com.abc.ssm.controller.ItemsController1"/>
<!-- 处理器映射器: 将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url)-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!--简单 url映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 对ItemsController1进行url映射queryItems1.action和queryItems2.action -->
<prop key="queryItems1.action">itemsController1</prop>
<prop key="queryItems2.action">itemsController1</prop>
</props>
</property>
</bean>
<!-- 处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
</bean>
<!-- 视图解析器: 解析jsp,默认使用jstl,classpath下要有jstl的包 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
5.实体类Items.Java:
package com.abc.ssm.po;
import java.util.Date;
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
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 == null ? null : name.trim();
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic == null ? null : pic.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail == null ? null : detail.trim();
}
}
6.开发非注解Handler(使用非注解的映射器和非注解的适配器)
控制器ItemsController1:
package com.abc.ssm.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.abc.ssm.po.Items;
public class ItemsController1 implements Controller {
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//1调用Service查询数据库商品列表,此处用静态数据模拟
List<Items> itemsList= new ArrayList<Items>();
Items items_1 = new Items();
items_1.setName("联想笔记本");
items_1.setPrice(4000f);
items_1.setCreatetime(new Date());
items_1.setDetail("ThinkPad");
Items items_2 = new Items();
items_2.setName("华为手机");
items_2.setPrice(4000f);
items_2.setCreatetime(new Date());
items_2.setDetail("华为P10");
itemsList.add( items_1);
itemsList.add( items_2);
//2准备ModelAndView
ModelAndView modelAndView = new ModelAndView();
//在jsp中通过“itemsList”取数据
modelAndView.addObject("itemsList",itemsList);
//3指定视图
String viewName = "/WEB-INF/jsp/items/itemsList.jsp";
modelAndView.setViewName(viewName);
return modelAndView;
}
}
7.JSP页面itemsList.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"%>
<%@page isELIgnored="false"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/queryItems.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/editItems.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
8.运行路径:http://localhost:8080/SpringMVC_01/queryItems.action
9.运行结果: