注:springmvc的注解方式从3.1开始需要在tomcat7上运行,tomcat7以下的版本会不支持此种方式,出现文件找不到的异常。
第一步:配置web.xml
<!-- 配置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>
第二步:编辑springmvc.xml文件,有关springmvc的配置,都在此文件中 ,包括前端控制器(DispatcherServlet)的配置、处理器适配器(HandlerAdapter)的配置、处理器映射器(HandlerMapping)的配置以及视图解析器(ViewResolver)的配置等
<?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"
<span style="background-color: rgb(255, 255, 102);">xmlns:mvc="http://www.springframework.org/schema/mvc"</span>
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 -->
<bean id="itermsController1" name="/queryItems.action" class="com.sky.ssm.controller.ItermsController1" />
<!-- 配置Handler2 -->
<bean id="itermsController2" class="com.sky.ssm.controller.ItermsController2" />
<!-- 配置注解的Handler -->
<!-- 对于注解的Handler可以单个配置
实际开发中,建议使用组件扫描
-->
<!-- <bean class="com.sky.ssm.controller.ItermsControllerAnnotation1"/> -->
<!-- context:component-scan可以扫描controller、service、... ...
这里让其扫描controller,指定controller的包
-->
<context:component-scan base-package="com.sky.ssm.controller"></context:component-scan>
<!-- 配置 处理器映射器
所有的处理器映射器都实现了HandlerMapping接口
-->
<!-- 多个处理器映射器可以并存,前端控制器 判断url能让哪个映射器映射,就让哪个正取的映射器处理-->
<!-- a:非注解的 -->
<!-- 此handlerMapping将bean的name作为url进行查找Handler,因此需要在配置Handler时指定beanname(就是url) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 简单url映射 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<!-- 此handlerMapping对 id为itermsController1的bean进行url映射,url是queryItems1.action -->
<prop key="/queryItems1.action">itermsController1</prop>
<prop key="/queryItems2.action">itermsController1</prop>
<prop key="/queryItems3.action">itermsController2</prop>
</props>
</property>
</bean>
<!-- b:注解的 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- mvc的注解驱动 -->
<!-- 使用mvc:annotation-driven代替注解的处理器映射器和处理器适配器的配置
mvc:annotation-driven会默认加载很多的参数绑定的方法。
比如json转换解析器就默认加载了,如果使用mvc:annotation-driven进行配置,就不用配置RequestMappingHandlerMapping和RequestMappingHandlerAdapter
在实际开发中 使用mvc:annotation-driven进行注解的配置
-->
<!-- <mvc:annotation-driven></mvc:annotation-driven> -->
<!-- 配置 处理器适配器
所有的处理器适配器都实现HandlerAdapter接口
多个适配器可以并存
-->
<!-- a:非注解的 -->
<!-- 此Adapter要求编写的handler需要实现Controller接口 -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- 此Adapter要求编写的handler实现HttpRequestHandler接口-->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/>
<!-- b:注解的 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 配置视图解析器
解析jsp视图,默认使用jstl标签,要求classpath下要有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
第三步:根据配置编写响应的视图和handler
1、配置文件中的几个handler的编写
ItermsController1.java
/**
* 非注解的Handler开发
* 实现controller接口的处理器
* @author sk
*
*/
public class ItermsController1 implements Controller{
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//调用service查找数据库,查询商品列表,这里使用静态数据模拟
List<Items> itemList = new ArrayList<Items>();
Items item1 = new Items();
item1.setName("联想笔记本1");
item1.setPrice(5000f);
item1.setDetail("thinkpad t430联想笔记本电脑!");
Items item2 = new Items();
item2.setName("联想笔记本2");
item2.setPrice(5000f);
item2.setDetail("thinkpad t430联想笔记本电脑!");
Items item3 = new Items();
item3.setName("联想笔记本3");
item3.setPrice(5000f);
item3.setDetail("thinkpad t430联想笔记本电脑!");
itemList.add(item1);
itemList.add(item2);
itemList.add(item3);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute,在jsp页面中通过itemList获取数据
modelAndView.addObject("itemList",itemList );
//指定视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
ItermsController2.java
<pre name="code" class="java">/**
* 非注解的Handler开发
* 实现HttpRequestHandler接口的处理器
* @author sk
* 通过此种方法可以通过修改response,设置响应的数据格式,比如返回json数据
* response.setCharacterEncoding("utf-8");
* response.setContentType("application/json;charset=utf-8");
* response.getWriter().write("json串");
*/
public class ItermsController2 implements HttpRequestHandler{
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//调用service查找数据库,查询商品列表,这里使用静态数据模拟
List<Items> itemList = new ArrayList<Items>();
Items item1 = new Items();
item1.setName("联想笔记本1");
item1.setPrice(5000f);
item1.setDetail("thinkpad t430联想笔记本电脑!");
Items item2 = new Items();
item2.setName("联想笔记本2");
item2.setPrice(5000f);
item2.setDetail("thinkpad t430联想笔记本电脑!");
Items item3 = new Items();
item3.setName("联想笔记本3");
item3.setPrice(5000f);
item3.setDetail("thinkpad t430联想笔记本电脑!");
itemList.add(item1);
itemList.add(item2);
itemList.add(item3);
//设置模型数据
request.setAttribute("itemList",itemList);
//设置转发的视图
request.getRequestDispatcher("/WEB-INF/jsp/items/itemsList.jsp").forward(request, response);
}
}
<pre name="code" class="java">ItermsControllerAnnotation1.java
/**
* 使用注解方式开发Handler
* 注意:使用注解的处理器适配器一定要使用注解的处理器映射器,即注解的映射器和适配器需要同时使用
*/
//使用@Controller标识它是一个控制器
@Controller
public class ItermsControllerAnnotation1 {
//查询商品列表的方法
//@RequestMapping实现对queryItemsAnnotation方法和url进行映射,一个方法对应一个url
//一般建议将url和方法名写成一样
@RequestMapping("/queryItemsAnnotation")
public ModelAndView queryItemsAnnotation(){
//调用service查找数据库,查询商品列表,这里使用静态数据模拟
List<Items> itemList = new ArrayList<Items>();
Items item1 = new Items();
item1.setName("联想笔记本1");
item1.setPrice(5000f);
item1.setDetail("thinkpad t430联想笔记本电脑!");
Items item2 = new Items();
item2.setName("联想笔记本2");
item2.setPrice(5000f);
item2.setDetail("thinkpad t430联想笔记本电脑!");
Items item3 = new Items();
item3.setName("联想笔记本3");
item3.setPrice(5000f);
item3.setDetail("thinkpad t430联想笔记本电脑!");
itemList.add(item1);
itemList.add(item2);
itemList.add(item3);
//返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
//相当于request的setAttribute,在jsp页面中通过itemList获取数据
modelAndView.addObject("itemList",itemList );
//指定视图
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
return modelAndView;
}
}
2、视图的编写
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>
至此,springmvc中注解的和非注解方式的基本配置已经清楚了。下面是项目的结构目录
因为springmvc为spring框架中的一部分,因此jar包用spring中的jar包就可以了,再加上jstl.jar和commons-logging.jar
部署到tomcat7之后,通过访问http://localhost:8080/FristSpringmvcProjects/queryItems.action 或者 http://localhost:8080/FristSpringmvcProjects/queryItems1.action 或者 http://localhost:8080/FristSpringmvcProjects/queryItems2.action 或者 http://localhost:8080/FristSpringmvcProjects/queryItemsAnnotation.action url就可以了,其中前3个链接测试的是非注解的配置,最后一个链接 测试的是 注解的配置