在整合完成service后,就是springmvc的整合。
第一步:创建springmvc.xml,在此配置文件中配置springmvc所需要的处理器映射器、处理器适配器、视图解析器等
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
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/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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 配置Handler -->
<!-- context:component-scan可以扫描controller、service、... ...
这里让其扫描controller,指定controller的包
-->
<context:component-scan base-package="com.sky.ssm.controller"></context:component-scan>
<!-- mvc的注解驱动 -->
<!-- 使用mvc:annotation-driven代替注解的处理器映射器和处理器适配器的配置
mvc:annotation-driven会默认加载很多的参数绑定的方法。
比如json转换解析器就默认加载了,如果使用mvc:annotation-driven进行配置,就不用配置RequestMappingHandlerMapping和RequestMappingHandlerAdapter
在实际开发中 使用mvc:annotation-driven进行注解的配置
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器
解析jsp视图,默认使用jstl标签,要求classpath下要有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp访问路径的前缀和后缀 在程序中就不用写jsp路径的前缀和后缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
第二步:配置前端控制器
在web.xml中配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringmvcAndMybatis</display-name>
<!-- 配置springmvc 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation参数配置的是springmvc加载时的配置文件(配置处理器、映射器、适配器等)
如果不配置contextConfigLocation,默认加载的是/WEB-INF/servelt名称-serlvet.xml(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>
<!--
第一种:*.action:所有访问的以.action结尾 的地址 交 由DispatcherServlet进行解析
第二种:/:所有访问的地址都由DispatcherServlet进行解析,对于静态文件的解析,需要配置,不让DispatcherServlet进行解析
使用此种方式可以实现RESTful风格的url
第三种:/*:这样配置不对,使用这种配置,最终要转发到一个jsp页面时,仍然会由DispatcherServlet解析jsp地址,不能根据jsp页面找到handler,会报错
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
第三步:编写controller(也就是handler)
package com.sky.ssm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.sky.ssm.po.ItemsCustom;
import com.sky.ssm.service.ItemsService;
/**
* 注解的Handler开发
*
* @author sk
*
*/
@Controller
public class ItermsController {
@Autowired
private ItemsService itemsService;
// 商品查询
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception {
// 调用service查找数据库,查询商品列表,
List<ItemsCustom> itemList = itemsService.findItemsList(null);
// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 相当于request的setAttribute,在jsp页面中通过itemList获取数据
modelAndView.addObject("itemList", itemList);
// 指定视图
// 下边的路径,如果在视图解析器中配置了jsp路径的前缀和后缀,则可以修改为
// modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
modelAndView.setViewName("/items/itemsList");
// 上面的路径配置,可以不再程序中指定jsp地址的前缀和后缀
return modelAndView;
}
}
第四步:编写view
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" %>
<!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=ISO-8859-1">
<title>商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/item/queryItem.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="${itemList }" 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 }/item/editItem.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
第五步:加载spring容器
将mapper、service、controller加载到spring容器中,即将spring的配置文件加载到spring容器中
可以在spring配置文件中添加include包含,引用其他的配置文件,不过,建议使用通配符的方法,来加载上面的配置文件。
需要在web.xml中,添加spring容器的监听器,加载spring容器
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
至此,整个springmvc+mybatis的整合工作就已经完成了。
项目结构目录
整合项目所用到的jar包下载地址:点击打开链接