SpringMVC介绍
SpringMVC是什么?
SpringMVC和Struts2都属于表现层的框架,它是Spring框架的一部分,我们可以从Spring的整体结构中看得出来:
SpringMVC处理流程
SpringMVC处理流程如下图所示:
这个图大致描述了SpringMVC的整个处理流程,乍一看有点晕乎,且待我一步步分析,最后弄个流程图出来就明白了。
SpringMVC入门程序
本系列教程使用的是SpringMVC4.1.3这个版本。下面我就来教大家如何入门SpringMVC这个框架。
现有这样一个需求:使用SpringMVC这个框架实现商品列表的展示。这是我对这个需求的分析:我这里假设请求的url为/itemList.action,由于我想要展示商品列表,所以是并不需要传递参数的,再次是这里仅仅是一个SpringMVC的一个入门小程序,并不会与MyBatis进行整合,也就不会从数据库表里面查询商品列表信息,故查询商品列表数据也仅仅只是一些静态数据。下面正式开始SpringMVC的入门小程序。
SpringMVC入门程序的开发步骤
【第一步】,创建一个javaweb工程,例如springmvc-first。
【第二步】,导入SpringMVC独立运行的jar包,如下:
【第三步】,创建一个jsp页面——itemList.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=UTF-8">
<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 }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
并把该jsp页面复制到工程的/WEB-INF/jsp目录下。
【第四步】,创建一个Item类,用于描述商品信息,其内容如下:
public class Items {
private int id;
private String name;
private double price;
private Date createtime;
private String detail;
public Items(int id, String name, double price, Date createtime, String detail) {
super();
this.id = id;
this.name = name;
this.price = price;
this.createtime = createtime;
this.detail = detail;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
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;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
并将该类复制到工程src目录下的com.itheima.springmvc.pojo包中。
【第五步】,创建ItemController,ItemController是一个普通的java类,有点类似于Struts2中的Action,且不需要实现任何接口,只需要在类上添加@Controller注解即可。@RequestMapping注解指定请求的url,其中“.action”可以加也可以不加。在ModelAndView对象中,将视图设置为“/WEB-INF/jsp/itemList.jsp”。
@Controller
public class ItemController {
// .action可以省略 (请求的url地址)
@RequestMapping("/itemList.action")
public ModelAndView itemList() {
// 查询商品列表,使用静态数据生成一个商品列表
List<Items> itemList = new ArrayList<Items>();
itemList.add(new Items(1, "imac", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(2, "imac1", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(3, "imac2", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(4, "imac3", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(5, "imac4", 20000, new Date(), "卧槽,苹果本很贵啦!"));
// 把商品列表传递给jsp
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
// 设置展示数据的视图,即jsp
modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
// 返回结果
return modelAndView;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
最后将ItemController类复制到工程src目录下的com.itheima.springmvc.controller包中。
【第六步】,创建springmvc.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.itheima.springmvc.controller"/>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
上面配置了扫描包(Controller类所在的包),那么它就会扫描这个包下所有带@Controller注解的类,并创建对象放到springmvc容器中。
【第七步】,配置前端控制器。在web.xml中添加DispatcherServlet的配置,即在web.xml文件中添加如下配置:
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定springmvc配置文件的路径。如果不指定,默认为:/WEB-INF/${servlet-name}-servlet.xml -->
<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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
【第八步】,入门程序测试。在浏览器地址栏中输入url地址——http://localhost:8080/springmvc-first/itemList.action,回车,就能看到如下效果:
相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了。还记得上一篇文章中SpringMVC的处理流程吗?
这个图大致描述了SpringMVC的整个处理流程,这个流程图还是相对来说比较容易懂的,下面我会给出SpringMVC的框架结构图,这个图相对来说会更复杂,不过经过我细致入微的讲解,相信大家都可以深入理解。
SpringMVC框架结构
SpringMVC框架结构如下图所示:
下面我会对SpringMVC框架结构作细致入微的讲解。
架构流程
- 用户发送请求至前端控制器DispatcherServlet。
- DispatcherServlet收到请求调用HandlerMapping处理器映射器。
- 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet。
- DispatcherServlet通过HandlerAdapter处理器适配器调用处理器。
- 执行处理器(Controller,也叫后端控制器)。
- Controller执行完成返回ModelAndView。
- HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。
- DispatcherServlet将ModelAndView传给ViewReslover视图解析器。
- ViewReslover解析后返回具体View。
- DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)。
- DispatcherServlet响应用户。
上面大致上对SpringMVC框架结构做了一个粗略的解释,下面我则会做一个更加细致入微的讲解。
- 浏览器发送请求,请求具体发到谁呢?先发到前端控制器,也就是说所有的请求都给发到前端控制器,前端控制器是所有请求的入口,但前端控制器不能处理业务请求,它只是一个请求的转发。
- 谁来处理业务请求呢?Handler处理器来真正处理业务请求,那么问题来了,前端控制器如何来找到这个Handler处理器呢?处理器映射器记录的就是请求的url和处理的方法之间的映射关系,这个映射关系是怎么建立起来的呢?就是通过@RequestMapping这个注解来建立起来的,这个映射关系就相当于一个Map(key-value这种形式),key就是请求的url,value就是处理的Handler。现在,前端控制器拿到这个请求之后,要找到对应的Handler,怎么找呢?就要找处理器映射器,问它请求谁来处理?
- 处理器映射器会根据你请求的url来找对应的处理器,找不到就会报错,如果找到之后,这时,它就会返回一个处理器执行链,这个处理器执行链里面除了有Handler之外,还有拦截器(这儿我们可以开发自己的拦截器),然后返回给前端控制器。
- 前端控制器依然不能处理这个业务请求,它这时做的还有另外一件事情,因为返回Handler,它也不知道这个Handler是什么类型,因为在spring mvc中Handler除了可以是注解形式的之外,其实还可以是非注解形式的(非注解形式我们一般不用),前端控制器并不知道这个Handler到底是什么类型的,那就没办法执行它,那总得找个东西执行,这时它就会把这个事交给另外一个组件来处理,这个组件就叫处理器适配器,这个处理器适配器就是来适配不同类型的Handler。它就会根据你不同类型的Handler来选择不同类型的适配器来执行它。
- 假如当前Handler是注解形式的,那么它就会选择注解形式的处理器适配器来执行这个Handler。Handler就执行了,也就是说我们Controller类中的那个方法就执行了,方法执行之后,里面的业务就处理了。
- 业务处理之后,最后返回一个ModelAndView。处理器适配器拿到这个结果是没有用的,它的作用就是执行这个Handler,把这个Handler执行完之后,它的事就做完了。
- 做完之后,拿到这个返回结果,那么它会原封不动地把这个返回结果扔给前端控制器,这时处理器适配器的事就做完了。
- 前端控制器拿到这个ModelAndView,它还是没有办法处理,它还是不能返回html,这时它要找到相应的jsp,因为ModelAndView即包含模型又包含视图,这个视图指定我们要用谁来渲染这个数据。我们要渲染数据,这时它就要找一个视图解析器来解析这个视图,由于这个视图也有很多种(我们最常见的视图是jsp,除了jsp,其实还有其他的,比如说还可以是报表,还可以是pdf,还可以是freemaker等),它会找不同的视图解析器来处理。因为现在我们最常用的视图是jsp,所以它就找到jsp对应的视图解析器。
- 找到这个视图解析器,它来把这个视图解析,解析完了之后它会返回一个View对象。
- 最后我们再调用这个视图解析器的渲染视图这个过程,渲染视图这个过程其实就是对于我们的jsp来说,就是把这个数据渲染成html。
- 最终渲染成html之后,就响应给用户。
通过以上的分析大家有没有更好地理解SpringMVC的架构流程呢?我可能写的比较绕口,但确是对我来说比较好理解一点,希望对你也是一样。
组件说明
以下组件通常使用框架提供实现:
- DispatcherServlet:前端控制器
用户请求到达前端控制器,它就相当于mvc模式中的c,DispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户的请求,DispatcherServlet的存在降低了组件之间的耦合性。 - HandlerMapping:处理器映射器
HandlerMapping负责根据用户请求找到Handler即处理器,springmvc提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。 - Handler:处理器
Handler是继DispatcherServlet前端控制器的后端控制器,在DispatcherServlet的控制下Handler对具体的用户请求进行处理。由于Handler涉及到具体的用户业务请求,所以一般情况需要程序员根据业务需求开发Handler。 - HandlAdapter:处理器适配器
通过HandlerAdapter对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。 - ViewResolver:视图解析器
ViewResolver负责将处理结果生成View视图,ViewResolver首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户。 - View:视图
SpringMVC框架提供了很多的View视图类型的支持,包括:jstlView、freemarkerView、pdfView等。我们最常用的视图就是jsp。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。
总结
在SpringMVC的各个组件中,处理器映射器、处理器适配器、视图解析器称为springmvc的三大组件。其实真正需要程序员开发的就两大块:一个是Handler,一个是jsp,这样的话,其实和Struts2就差不多了,所以不要被上面那个一系列的流程给绕傻了。
SpringMVC学习(三)——SpringMVC的配置文件
读者阅读过SpringMVC学习(一)——SpringMVC介绍与入门这篇文章后,想必都会写写SpringMVC的入门小程序,在这个小程序中,SpringMVC的核心配置文件——springmvc.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.itheima.springmvc.controller"/>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
读者可能怀疑这写的不对啊!怎么可能只配这点东西呢?SpringMVC的三大组件哪去了,它们不是要配置吗?且听我慢慢讲解。我们发现这几个组件并没配置,但却是好使的,就是因为它有一个默认配置,DispatcherServlet.properties这个默认配置文件里面默认加载了,看图:
可以看出我们使用了注解方式的处理器映射器和处理器适配器。
默认加载的注解方式的处理器映射器
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping- 1
默认加载的注解方式的处理器适配器
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter- 1
默认加载的视图适配器(默认解析JSP视图的视图解析器)
org.springframework.web.servlet.view.InternalResourceViewResolver- 1
我们如果使用默认加载的注解方式的映射器和适配器,那么对它们的可控制性是比较小的,所以一般来说,我们都是自己配置的,因为有的时候我们需要扩展一些其他的组件。
注解映射器和适配器
配置组件扫描器
使用组件扫描器可省去在Spring容器中配置每个Controller类的繁琐。使用<context:component-scan>自动扫描标记@controller注解的控制器类,配置如下:
<context:component-scan base-package="com.itheima.springmvc.controller"/>
- 1
注意:如果要扫描多个包,多个包中间使用半角逗号分隔。很明显在入门小程序中我已经配置了。
配置RequestMappingHandlerMapping
注解式处理器映射器,对类中标记@ResquestMapping注解的方法进行映射,根据@ResquestMapping注解定义的url匹配@ResquestMapping注解标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装了url对应的方法Method。
从Spring3.1版本开始,废除了DefaultAnnotationHandlerMapping的使用,推荐使用RequestMappingHandlerMapping完成注解式处理器映射。配置如下:
<!-- 配置注解式处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
- 1
- 2
推荐使用最新版本的注解式处理器映射器,如果你想对其扩展,可以在这个bean里面配置其他的属性。 @RequestMapping注解的描述:定义请求url到处理器功能方法的映射。
配置RequestMappingHandlerAdapter
注解式处理器适配器,对标记@ResquestMapping注解的方法进行适配。
从Spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使用RequestMappingHandlerAdapter完成注解式处理器适配。配置如下:
<!-- 配置注解式处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
- 1
- 2
推荐使用最新版本的注解式处理器适配器,如果你想对其扩展,可以在这个bean里面配置其他的属性。
当我们配置完注解式处理器映射器和注解式处理器适配器之后,SpringMVC的核心配置文件——springmvc.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.itheima.springmvc.controller"/>
<!-- 配置注解式处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置注解式处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
然后在浏览器地址栏中输入url地址——http://localhost:8080/springmvc-first/itemList.action,回车,也能同样看到如下效果:
继续优化注解,配置<mvc:annotation-driven>
使用注解要注意一个问题,就是注解适配器和映射器必须配对使用,也就是说,不能一个用注解,一个用非注解。要用一起用,要么都不用。其实在SpringMVC中还有更加简便的注解,SpringMVC使用<mvc:annotation-driven>自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter,可在springmvc.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置,如下图所示:
注意:如果配置一个注解驱动之后,那么就可以不用配置处理器映射器和处理器适配器了。
此时在浏览器地址栏中输入url地址——http://localhost:8080/springmvc-first/itemList.action,回车,同样也能看到如上效果。
配置视图解析器
我们也可在springmvc.xml配置文件中自己手动配置视图解析器,如下:
<!-- 配置视图解析器(对jsp默认解析的视图解析器) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- prefix:前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"></property>
<!-- suffix:后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- InternalResourceViewResolver:支持JSP视图解析。
- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar 包。此属性可以不设置,默认为JstlView。
- prefix和suffix:查找视图页面的前缀和后缀,最终视图的址为:前缀+逻辑视图名+后缀,逻辑视图名需要在Controller返回的ModelAndView中指定,比如逻辑视图名为hello,则最终返回的jsp物理视图地址就为 “WEB-INF/jsp/hello.jsp”。
这样一来,ItemController类的代码就要修改为:
@Controller
public class ItemController {
// .action可以省略 (请求的url地址)
@RequestMapping("/itemList.action")
public ModelAndView itemList() {
// 查询商品列表,使用静态数据生成一个商品列表
List<Items> itemList = new ArrayList<Items>();
itemList.add(new Items(1, "imac", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(2, "imac1", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(3, "imac2", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(4, "imac3", 20000, new Date(), "苹果本很贵"));
itemList.add(new Items(5, "imac4", 20000, new Date(), "卧槽,苹果本很贵啦!"));
// 把商品列表传递给jsp
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList);
// 配置完视图解析器之后只需要返回返回jsp的名称即可
modelAndView.setViewName("itemList");
// 返回结果
return modelAndView;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
到这就基本总结完了SpringMVC中使用注解方式的适配器和映射器了,很明显,开发中我们就使用注解配置,那样非常方便
SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合
之前我整合了Spring和MyBatis这两个框架,不会的可以看我的文章MyBatis框架的学习(六)——MyBatis整合Spring。本文我再来讲SpringMVC和MyBatis整合开发的方法,这样的话,Spring、MyBatis和SpringMVC三大框架的整合开发我们就学会了。这里我使用的Spring是Spring4.1.3这个版本(SpringMVC自然也是这个版本),MyBatis是MyBatis3.2.7这个版本。
为了更好的学习SpringMVC和MyBatis整合开发的方法,需要将SpringMVC和MyBatis进行整合。整合目标:控制层采用SpringMVC、持久层使用MyBatis实现。
需求
实现商品查询列表,从mysql数据库表中查询商品信息。
为了实现这个需求,我们需要新建一个数据库,例如springmvc,然后将以下sql脚本文件导入springmvc数据库中:
springmvc.sql:
/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50611 Source Host : localhost:3306 Source Database : springmvc Target Server Type : MYSQL Target Server Version : 50611 File Encoding : 65001 Date: 2016-05-09 19:45:13 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for items -- ---------------------------- DROP TABLE IF EXISTS `items`; CREATE TABLE `items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL COMMENT '商品名称', `price` float(10,1) NOT NULL COMMENT '商品定价', `detail` text COMMENT '商品描述', `pic` varchar(64) DEFAULT NULL COMMENT '商品图片', `createtime` datetime NOT NULL COMMENT '生产日期', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of items -- ---------------------------- INSERT INTO `items` VALUES ('1', '台式机', '3000.0', '该电脑质量非常好!!!!', null, '2016-02-03 13:22:53'); INSERT INTO `items` VALUES ('2', '笔记本', '6000.0', '笔记本性能好,质量好!!!!!', null, '2015-02-09 13:22:57'); INSERT INTO `items` VALUES ('3', '背包', '200.0', '名牌背包,容量大质量好!!!!', null, '2015-02-06 13:23:02'); -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) NOT NULL COMMENT '用户名称', `birthday` date DEFAULT NULL COMMENT '生日', `sex` char(1) DEFAULT NULL COMMENT '性别', `address` varchar(256) DEFAULT NULL COMMENT '地址', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=27 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of user -- ---------------------------- INSERT INTO `user` VALUES ('1', '王五', null, '2', null); INSERT INTO `user` VALUES ('10', '张三', '2014-07-10', '1', '北京市'); INSERT INTO `user` VALUES ('16', '张小明', null, '1', '河南郑州'); INSERT INTO `user` VALUES ('22', '陈小明', null, '1', '河南郑州'); INSERT INTO `user` VALUES ('24', '张三丰', null, '1', '河南郑州'); INSERT INTO `user` VALUES ('25', '陈小明', null, '1', '河南郑州'); INSERT INTO `user` VALUES ('26', '王五', null, null, null);- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
如此一来items表就是这样的:
由于需求决定,我们只对items表进行操作。
jar包管理
我之前有写过一篇Spring、Hibernate和Struts2整合的文章——hibernate-5.0.7+struts-2.3.24+spring-4.2.4三大框架整合,在整合的时候,我个人不喜欢乱,不喜欢啪叽一下将所有jar包往lib中一扔,因为那样没有条理,所以在整合SSM的时候,我还是遵循jar包分类的原则,首先看一下SSM整合都用到了哪些jar包:
- Spring(包括SpringMVC)所需jar包
- MyBatis所需jar包
- mybatis-spring整合包
- 数据库驱动包
- 第三方数据库连接池
这样一共需要31个jar包,我整理出来如下:
这里我用的是dbcp数据库连接池,当然也可以用c3p0等其他连接池,归归类后jar包就很有条理。
整合思路
关于SSM的架构可以简单看一下下面的草图:
可以看出,Spring在进行管理时,是很有条理的,每个层都由Spring管理,然后不同的层可以调用其它层,Handler调用service,service调用mapper等。根据这个架构,我们来总结一下整合的思路,根据这个调用关系,我们可以从下往上一步步整合。
- Dao层
- SqlMapConfig.xml,空文件即可。
- applicationContext-dao.xml
- a) 数据库连接池
- b) SqlSessionFactory对象,需要Spring和MyBatis的整合包。
- c) 配置mapper文件扫描器。
- Service层
- applicationContext-service.xml文件中配置包扫描器,扫描带@service注解的类。
- applicationContext-trans.xml文件中配置事务。
- 表现层
- 包扫描器,扫描带@Controller注解的类。
- 配置注解驱动。
- 配置视图解析器
- web.xml
在web.xml文件中配置前端控制器。
现在思路清晰了,接下来就开始整合了。在整合前先看一下我整合完的工程结构:
整合Dao层
整合Dao层也就是整合持久层,那么需要Spring的核心包,持久层包,MyBatis包,数据库以及连接池的包。
MyBatis全局配置文件
在classpath下创建mybatis/sqlMapConfig.xml这样的MyBatis全局配置文件,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
- 1
- 2
- 3
- 4
- 5
- 6
可以看出,整合的时候,这个全局配置文件已经很清爽了,根本就没啥东东了,因为数据源啊、mapper啊啥的都交给Spring去管理了。
配置Spring配置文件
配置完MyBatis的全局配置文件后,接下来就要配置Spring的配置文件了,Spring的配置文件我将分类写在不同的文件中,都放在config/spring/目录下了,这里是对Dao的整合,所以起名applicationContext-dao.xml。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件,虽然这个全局配置文件是空的,但是这个全局配置文件是必不可少的 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
<!-- 配置Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.springmvc.mapper"/>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
注意:一定记得加载mybatis的全局配置文件,虽然这个全局配置文件是空的,但是这个全局配置文件是必不可少的。
可以看出,整合Dao层的时候主要配置了一下数据源、sqlSessionFactory和mapper扫描器, 这样的话,数据源,sqlSessionFactory和mapper在tomcat启动时就被Spring实例化到了容器中。
这儿db.properties该属性配置文件的内容为:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
jdbc.username=root
jdbc.password=yezi
- 1
- 2
- 3
- 4
整合Service层
先把jar包导了再说,整合Service层需要配置事务了。
配置applicationContext-service.xml
这里是第二个Spring的配置文件了,还是在config/spring文件夹下面,主要是用来配置所有的service的,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 配置包扫描器,扫描带@Service注解的类 -->
<context:component-scan base-package="com.itheima.springmvc.service"></context:component-scan>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
这儿只须配置包扫描器,专门扫描带@Service注解的类。
配置applicationContext-trans.xml
这里是第三个Spring的配置文件了,还是在config/spring文件夹下面,主要是用来配置Spring事务管理的,如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 事务管理器,用的是Spring JDBC的事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.itheima.springmvc.service.*.*(..))" />
</aop:config>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
有关Spring中事务的传播行为,我也不是特别了解。也只是听说过一嘴,先记录如下:
- propagation=”REQUIRED”:如果当前方法执行的时候,没有事务要开启事务,如果有事务,就在当前事务中执行。也可理解为当前方法被调用的时候没有事务传递进来,就自己开启事务。
- propagation=”SUPPORTS”:如果当前方法执行的时候,如果有事务,就在当前事务中执行,如果没有事务就不开启事务。也可理解为当前方法被调用的时候没有事务传递进来,自己不开启事务。
整合表现层
上面提到过,SpringMVC是Spring的一个模块,所以不需要整合,我们只需要加入SpringMVC所需的jar包即可。
配置处理器映射器、处理器适配器和视图解析器
这里使用注解的方式配置,因为注解的方式比较简单。如此一来SpringMVC配置文件——springmvc.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.itheima.springmvc.controller"/>
<!-- 配置一个注解驱动,如果配置此标签,那么就可以不用配置处理器映射器和处理器适配器 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
该配置文件依然在config/spring文件夹下面。
配置前端控制器
前端控制器要配置在WEB-INF/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"
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>springmvc-web</display-name>
<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>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定springmvc配置文件的路径。如果不指定,默认为:/WEB-INF/${servlet-name}-servlet.xml -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
加载Spring容器
注意千万不要忘记了在web.xml中添加Spring容器监听器,加载Spring容器:
<!-- 初始化Spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
到此为止,Spring、MyBatis和SpringMVC三大框架就整合完了。整合完之后,接着就来实现我们的需求——商品列表的展示。
商品列表的展示
Dao层代码的编写,借助逆向工程生成po类及mapper
关于如何使用MyBatis的逆向工程我就不再赘述了,如果不太清楚的童鞋请看一下我的这篇文章——MyBatis框架的学习(七)——MyBatis逆向工程自动生成代码!将生成的代码拷贝到我们自己的工程中即可,如下:
Service层代码的编写
之前提到过,service是用来调用mapper的,mapper是用来操作数据库的,所以在Service层,我们首先要获取mapper接口的代理对象,只不过在这里我们通过Spring注入进来,然后通过这个代理对象去操作数据库。
【第一步】,首先在com.itheima.springmvc.service包下编写一个ItemService接口,如下:
public interface ItemService {
List<Items> getItemList();
}
- 1
- 2
- 3
可以看出,这个接口和上面那个mapper接口其实是一样的,当然并不是说一定一样,只不过这里要实现的逻辑都一样而已。
【第二步】,在com.itheima.springmvc.service.impl包下编写ItemService接口的实现类——ItemServiceImpl.java,如下:
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemsMapper itemsMapper;
@Override
public List<Items> getItemList() {
ItemsExample example = new ItemsExample();
List<Items> list = itemsMapper.selectByExampleWithBLOBs(example);
return list;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
注意:selectByExampleWithBLOBs指代带大文本那一列(即detail列)的方法。从items这个数据库表的建表语句中可看出detail这列的数据类型是text,如下:
Web层代码的编写
在com.itheima.springmvc.controller包下编写一个Controller类,如下:
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/itemList")
public ModelAndView getItemsList() {
// 查询商品列表
List<Items> itemList = itemService.getItemList();
// 把查询结果传递给页面
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList); // addObject方法相当于放到request域上
// 设置逻辑视图
modelAndView.setViewName("itemList");
// 返回结果
return modelAndView;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
前台itemList.jsp页面还是第一篇博文中写的那个,没有变,就不写了。
测试
最后在浏览器地址栏中输入url地址——http://localhost:8080/springmvc-web/itemList.action,回车,即可看到如下效果:
这已然充分说明Spring、MyBatis和SpringMVC这三大框架就整合成功了!!!事实雄于一切。
SpringMVC学习(五)——SpringMVC的参数绑定
SpringMVC中的参数绑定还是蛮重要的,所以单独开一篇文章来讲解。本文所有案例代码的编写均建立在前文SpringMVC学习(四)——Spring、MyBatis和SpringMVC的整合的案例基础之上,因此希望读者能仔细阅读这篇文章。
默认支持的数据类型
现在有这样一个需求:打开商品编辑页面,展示商品信息。这是我对这个需求的分析:编辑商品信息,需要根据商品id查询商品信息,然后展示到页面。我这里假设请求的url为/itemEdit.action,由于我想要根据商品id查询商品信息,所以需要传递商品id这样一个参数。最终的一个响应结果就是在商品编辑页面中展示商品详细信息,如下:
为了解决这个需求,必然要有一个商品编辑页面,这里将如下editItem.jsp复制到工程的/WEB-INF/jsp目录下。
商品编辑页面——editItem.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=UTF-8"> <title>修改商品信息</title> </head> <body> <!-- 上传图片是需要指定属性 enctype="multipart/form-data" --> <!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> --> <form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post"> <input type="hidden" name="id" value="${item.id }" /> 修改商品信息: <table width="100%" border=1> <tr> <td>商品名称</td> <td><input type="text" name="name" value="${item.name }" /></td> </tr> <tr> <td>商品价格</td> <td><input type="text" name="price" value="${item.price }" /></td> </tr> <tr> <td>商品简介</td> <td><textarea rows="3" cols="30" name="detail">${item.detail }</textarea> </td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="提交" /> </td> </tr> </table> </form> </body> </html>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
当然了,在商品列表展示页面——itemList.jsp中,我们还须注意编辑修改这个超链接,如下图所示:
所有前端页面准备好之后,接下来就要编写后台业务代码了。
编写Service层代码
首先在ItemService接口中添加如下一个方法:
Items getItemById(int id);
- 1
如此一来,ItemService接口的代码就变为:
public interface ItemService {
List<Items> getItemList();
Items getItemById(int id);
}
- 1
- 2
- 3
- 4
紧接着在ItemService接口的实现类——ItemServiceImpl.java中实现以上方法,即在ItemServiceImpl实现类中添加如下方法:
@Override
public Items getItemById(int id) {
// 根据商品id查询商品信息
Items items = itemsMapper.selectByPrimaryKey(id);
return items;
}
- 1
- 2
- 3
- 4
- 5
- 6
Controller类的参数绑定
要根据商品id查询商品数据,需要从请求的参数中把请求的id取出来。id应该包含在Request对象中。可以从Request对象中取id。因此我们应在ItemController类中添加如下方法:
public ModelAndView editItem(HttpServletRequest request) {
// 从request中取出参数
String strId = request.getParameter("id");
int id = new Integer(strId);
// 调用服务
Items items = itemService.getItemById(id);
// 把结果传递给页面
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("item", items);
// 设置逻辑视图
modelAndView.setViewName("editItem");
return modelAndView;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
如果想获得Request对象只需要在Controller类方法的形参中添加一个参数即可。SpringMVC框架会自动把Request对象传递给方法。这就是SpringMVC框架默认支持的参数类型。
SpringMVC框架默认支持的参数类型
处理器形参中添加如下类型的参数,处理适配器会默认识别并进行赋值。
- HttpServletRequest:通过request对象获取请求信息。
- HttpServletResponse:通过response处理响应信息。
- HttpSession:通过session对象得到session中存放的对象。
Model/ModelMap:ModelMap是Model接口的实现类,我们可通过Model或ModelMap向页面传递数据,如下:
Items items = itemService.getItemById(id); model.addAttribute("item", items);- 1
- 2
页面中通过${item.XXXX}获取item对象的属性值。
使用Model和ModelMap的效果是一样的,如果直接使用Model接口,SpringMVC会实例化ModelMap。如果使用Model接口,那么editItem方法可以改造成:@RequestMapping("/itemEdit") public String editItem(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) { // 从request中取出参数 String strId = request.getParameter("id"); int id = new Integer(strId); // 调用服务 Items items = itemService.getItemById(id); // 使用模型设置返回结果,model是框架给我们传递过来的对象,所以这个对象也不需要我们返回 model.addAttribute("item", items); // 类似于:modelAndView.addObject("item", items); // 返回逻辑视图 return "editItem"; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
如果使用Model接口则可以不使用ModelAndView对象,Model对象可以向页面传递数据(model是框架给我们传递过来的对象,所以这个对象不需要我们返回),View对象则可以使用String返回值替代。不管是Model还是ModelAndView,其本质都是使用Request对象向jsp传递数据。
简单数据类型绑定
当请求的参数名称和处理器形参名称一致时会将请求参数与形参进行绑定。从Request取参数的方法可以进一步简化。这样一来,editItem方法可以改造成:
@RequestMapping("/itemEdit")
public String editItem(Integer id, Model model) {
// 调用服务
Items items = itemService.getItemById(id);
// 把数据传递给页面,需要用到Model接口
model.addAttribute("item", items);
// 返回逻辑视图
return "editItem";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
支持的数据类型
SpringMVC框架支持的数据类型有:
- 整形:Integer、int
- 字符串:String
- 单精度:Float、float
- 双精度:Double、double
布尔型:Boolean、boolean
说明:对于布尔类型的参数,请求的参数值为true或false。处理器方法可是这样的:public String editItem(Model model,Integer id,Boolean status) throws Exception { ... }- 1
- 2
- 3
至于请求的url,可是
http://localhost:8080/xxx.action?id=2&status=false。
注意:参数类型推荐使用包装数据类型,因为基础数据类型不可以为null。
@RequestParam
使用@RequestParam注解常用于处理简单类型的绑定。
- value:参数名字,即入参的请求参数名字,如value=“item_id”表示请求的参数区中的名字为item_id的参数的值将传入。
- required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报如下错误:
- defaultValue:默认值,表示如果请求中没有同名参数时的默认值。
使用@RequestParam注解,editItem方法可以改造成:
@RequestMapping("/itemEdit")
public String editItem(@RequestParam(value="id",defaultValue="1",required=true) Integer ids, Model model) {
// 调用服务
Items items = itemService.getItemById(ids);
// 把数据传递给页面,需要用到Model接口
model.addAttribute("item", items);
// 返回逻辑视图
return "editItem";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
形参名称为ids,但是这里使用value=”id”限定请求的参数名为id,所以页面传递参数的名称必须为id。注意:如果请求参数中没有id将抛出异常:
这里通过required=true限定id参数为必须传递,如果不传递则报400错误,可以使用defaultvalue设置默认值,即使required=true也可以不传id参数值。
绑定pojo类型
现有这样一个需求:将页面修改后的商品信息保存到数据库表中。这是我对这个需求的分析:我这里假设请求的url为/updateitem.action,由于我想要将页面修改后的商品信息保存到数据库表中,所以需要传递的参数是表单中的数据。最终的一个响应结果就是跳转到更新成功页面。
使用pojo接收表单数据
如果提交的参数很多,或者提交的表单中的内容很多的时候可以使用pojo接收数据。要求pojo对象中的属性名和表单中input的name属性一致。就像下图所示:
编写Service层代码
首先在ItemService接口中添加如下一个方法:
void updateItem(Items items);
- 1
如此一来,ItemService接口的代码就变为:
public interface ItemService {
List<Items> getItemList();
Items getItemById(int id);
void updateItem(Items items);
}
- 1
- 2
- 3
- 4
- 5
紧接着在ItemService接口的实现类——ItemServiceImpl.java中实现以上方法,即在ItemServiceImpl实现类中添加如下方法:
@Override
public void updateItem(Items items) {
itemsMapper.updateByPrimaryKeySelective(items);
}
- 1
- 2
- 3
- 4
Controller类的参数绑定
在Controller类中使用pojo数据类型进行参数绑定,即应在ItemController类中添加如下方法:
@RequestMapping("/updateitem")
public String updateItems(Items items) {
itemService.updateItem(items);
// 返回成功页面
return "success";
}
- 1
- 2
- 3
- 4
- 5
- 6
请求的参数名称和pojo的属性名称一致,会自动将请求参数赋值给pojo的属性。注意:提交的表单中不要有日期类型的数据,否则会报400错误。如果想提交日期类型的数据需要用到后面的自定义参数绑定的内容。
最后的响应结果就是跳转到更新成功页面。所以还须在/WEB-INF/jsp目录下编写一个jsp页面——success.jsp,如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<h1>商品更新成功!!!</h1>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
这样,当我们修改完商品信息之后,去数据库表中瞧一瞧,就会看到中文乱码情况了,如下:
我们知道表单提交的方式是post,那么如何来解决post提交的中文乱码问题呢?我们可在web.xml文件中加入一个过滤器,如下:
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
注意:以上只可以解决post请求乱码问题。
对于get请求方式中文参数出现乱码解决方法有两个:
修改tomcat配置文件添加编码与工程编码一致,如下:
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>- 1
另外一种方法对参数进行重新编码:
String username = new String(request.getParamter("userName").getBytes("ISO8859-1"),"utf-8");- 1
ISO8859-1是tomcat默认编码,需要将tomcat编码后的内容按utf-8编码。
解决完post提交的中文乱码问题之后,再次修改商品信息,去数据库表中瞧一瞧,将发现一切正常。
绑定包装pojo
现有这样一个需求:使用包装的pojo接收商品信息的查询条件。下面是我的需求分析:
首先,在com.itheima.springmvc.pojo包下编写一个包装类,定义如下:
public class QueryVo {
private Items items;
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
然后在itemList.jsp页面中添加如下input输入项:
<input type="text" name="items.id" />
<input type="text" name="items.name" />
- 1
- 2
添加的位置我已在下图框出:
接着在ItemController类中添加如下方法:
@RequestMapping("/queryitem")
public String queryItem(QueryVo queryVo) {
// 打印绑定结果
System.out.println(queryVo.getItems().getId());
System.out.println(queryVo.getItems().getName());
return "success";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
最后的一个访问流程大致就是这样:
自定义参数绑定
有这样一个需求:在商品修改页面可以修改商品的生产日期,并且根据业务需求自定义日期格式。
要解决这个需求,首先要在editItem.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=UTF-8">
<title>修改商品信息</title>
</head>
<body>
<!-- 上传图片是需要指定属性 enctype="multipart/form-data" -->
<!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> -->
<form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post">
<input type="hidden" name="id" value="${item.id }" /> 修改商品信息:
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td><input type="text" name="name" value="${item.name }" /></td>
</tr>
<tr>
<td>商品价格</td>
<td><input type="text" name="price" value="${item.price }" /></td>
</tr>
<tr>
<td>商品生产日期</td>
<td><input type="text" name="createtime"
value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td>
</tr>
<tr>
<td>商品简介</td>
<td><textarea rows="3" cols="30" name="detail">${item.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
我们还是一如往前地修改商品的信息(当然包含修改商品的生产日期),当我们点击提交按钮时,发现报400错误,如下:
我之前就已讲过:提交的表单中不要有日期类型的数据,否则会报400错误。如果想提交日期类型的数据需要用到后面的自定义参数绑定的内容。所以要真正解决这个需求,就必然要用到自定义参数绑定的内容。下面是我对这个需求的分析:由于日期数据有很多种格式,所以SpringMVC没办法把字符串转换成日期类型。所以需要自定义参数绑定。前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestMapping标记的方法进行适配,并对方法中的形参进行参数绑定。在SpringMVC中可以在处理器适配器上自定义Converter进行参数绑定。如果使用<mvc:annotation-driven/>,可以在此标签上进行扩展。
自定义Converter
在com.itheima.springmvc.converter下编写一个自定义Converter——DateConverter.java,如下:
/**
* SpringMVC转换器
* Converter<S, T> S:source源数据类型,T:target目标数据类型
* @author 李阿昀
*
*/
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(source);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
配置Converter
如果使用<mvc:annotation-driven/>,可以在此标签上进行扩展。在springmvc.xml配置文件中添加如下配置:
<!-- 配置一个注解驱动,如果配置此标签,那么就可以不用配置处理器映射器和处理器适配器 -->
<mvc:annotation-driven conversion-service="conversionService" />
<!-- 转换器的配置 -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.itheima.springmvc.converter.DateConverter"/>
</set>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
其实配置Converter还有另一种方式,不过实际开发中用到的很少,这里还是讲一下,按照这种方式配置完Converter之后,springmvc.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:p="http://www.springframework.org/schema/p"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 配置一个扫描包,指定Controller的包,它会扫描这个包下所有带@Controller注解的类,并创建对象放到springmvc容器中 -->
<context:component-scan base-package="com.itheima.springmvc.controller"/>
<!-- 转换器配置 -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.itheima.springmvc.converter.DateConverter"/>
</set>
</property>
</bean>
<!-- 自定义webBinder -->
<bean id="customBinder" class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService" ref="conversionService" />
</bean>
<!-- 注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder"></property>
</bean>
<!-- 注解处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
注意:此方法需要独立配置处理器映射器、适配器,不再使用<mvc:annotation-driven/>。
这样当我们修改商品的信息(当然包含修改商品的生产日期)时,就能修改成功了,并不会报400的错误。
SpringMVC与Struts2的不同
- SpringMVC的入口是一个servlet即前端控制器,而Struts2入口是一个filter过虑器。
- SpringMVC是基于方法开发(一个url对应一个方法),请求参数传递到方法的形参,可以设计为单例或多例(建议单例),Struts2是基于类开发,传递参数是通过类的属性,只能设计为多例。
- Struts2采用值栈存储请求和响应的数据,通过OGNL存取数据,SpringMVC通过参数解析器将request请求内容解析,并给方法形参赋值,将数据和视图封装成ModelAndView对象,最后又将ModelAndView中的模型数据通过request域传输到页面。jsp视图解析器默认使用jstl。
SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解
高级参数绑定
现在进入SpringMVC高级参数绑定的学习,本文所有案例代码的编写均建立在前文SpringMVC学习(五)——SpringMVC的参数绑定的案例基础之上,因此希望读者能仔细阅读这篇文章。
绑定数组
现有这样一个需求:在商品列表页面选中多个商品,然后删除之。下面是我对该需求的分析:此功能要求商品列表页面中的每个商品前有一个checkbook(复选框),选中多个商品后点击删除按钮把商品id传递给Controller,根据商品id批量删除商品信息。
首先将itemList.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=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td>商品id:<input type="text" name="items.id" /></td>
<td>商品名称:<input type="text" name="items.name" /></td>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>选择</td>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item">
<tr>
<td><input type="checkbox" name="ids" value="${item.id }" /></td>
<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 }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
生成html代码如下:
页面选中多个checkbox向Controller类的方法中传递。
接着要修改ItemController类的queryItem方法,接收itemList.jsp页面传递过来的多个商品id。Controller类方法中可以用String[]接收,或者pojo的String[]属性接收。两种方式任选其一即可。
方式一:使用String[]接收。
如果使用String[]接收,那么ItemController类的queryItem方法应改造为:@RequestMapping("/queryitem") public String queryItem(QueryVo queryVo, String[] ids) { // 打印绑定结果 System.out.println(queryVo.getItems().getId()); System.out.println(queryVo.getItems().getName()); return "success"; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
方式二:使用pojo的String[]属性接收。
首先在QueryVo类中添加如下属性:private String[] ids;- 1
并添加其相对应的get/set方法。如此一来,QueryVo类就改造为:
public class QueryVo { private Items items; private String[] ids; public String[] getIds() { return ids; } public void setIds(String[] ids) { this.ids = ids; } public Items getItems() { return items; } public void setItems(Items items) { this.items = items; } }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
ItemController类的queryItem方法不变,假设依然为:
@RequestMapping("/queryitem") public String queryItem(QueryVo queryVo, String[] ids) { // 打印绑定结果 System.out.println(queryVo.getItems().getId()); System.out.println(queryVo.getItems().getName()); return "success"; }- 1
- 2
- 3
- 4
- 5
- 6
- 7
在以下代码处打一个断点:
System.out.println(queryVo.getItems().getId());- 1
然后以Debug模式运行queryItem方法,观察queryVo和ids两个变量,效果如下:
将表单的数据绑定到List
现有这样一个需求:实现商品数据信息的批量修改。下面是我对该需求的分析:要想实现商品数据信息的批量修改,需要在商品列表中可以对商品信息进行修改,并且可以批量提交修改后的商品数据。
首先将QueryVo类改造为:
public class QueryVo {
private Items items;
private String[] ids;
private List<Items> itemList;
public List<Items> getItemList() {
return itemList;
}
public void setItemList(List<Items> itemList) {
this.itemList = itemList;
}
public String[] getIds() {
return ids;
}
public void setIds(String[] ids) {
this.ids = ids;
}
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
List中存放对象,并将定义的List放在包装类中,使用包装pojo对象接收。
然后将itemList.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=UTF-8">
<title>查询商品列表</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/queryitem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td>商品id:<input type="text" name="items.id" /></td>
<td>商品名称:<input type="text" name="items.name" /></td>
<td><input type="submit" value="查询"/></td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>选择</td>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item" varStatus="s">
<tr>
<td><input type="checkbox" name="ids" value="${item.id }" /></td>
<td>
<input type="hidden" name="itemList[${s.index}].id" value="${item.id }" />
<input type="text" name="itemList[${s.index}].name" value="${item.name }" />
</td>
<td><input type="text" name="itemList[${s.index}].price" value="${item.price }" /></td>
<td><input type="text" name="itemList[${s.index}].createtime" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td>
<td><input type="text" name="itemList[${s.index}].detail" value="${item.detail }" /></td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
注意:以上input输入项的name属性必须是包装pojo的list属性+下标+元素属性。
大家可能忘了<c:forEach>标签中的varStatus属性了,在此我对它做一个总结,希望能唤醒读者的记忆。
varStatus属性常用参数总结如下:
- ${status.index}:输出行号,从0开始。
- ${status.count}:输出行号,从1开始。
- ${status.current}:当前这次迭代的(集合中的)项。
- ${status.first}:判断当前项是否为集合中的第一项,返回值为true或false。
- ${status.last}:判断当前项是否为集合中的最后一项,返回值为true或false。
- begin、end、step分别表示:起始序号,结束序号,跳跃步伐。
最后并不需要修改ItemController类的queryItem方法,假设依然为:
@RequestMapping("/queryitem")
public String queryItem(QueryVo queryVo, String[] ids) {
// 打印绑定结果
System.out.println(queryVo.getItems().getId());
System.out.println(queryVo.getItems().getName());
return "success";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
注意:接收List类型的数据必须是pojo的属性,方法的形参为List类型无法正确接收到数据。
在以下代码处打一个断点:
System.out.println(queryVo.getItems().getId());
- 1
然后以Debug模式运行queryItem方法,观察queryVo这个变量,就能看到我们所想要的效果。
@RequestMapping注解的使用
之前,我们就已经使用过@RequestMapping注解了,但还有一些细节我还没讲到,所以在此将详细讲讲@RequestMapping注解的使用。
@RequestMapping
通过@RequestMapping注解可以定义不同的处理器映射规则。
URL路径映射
之前我们也经常编写URL路径映射,大致可以写为:
@RequestMapping("/item")
- 1
或者
@RequestMapping(value="/item")
- 1
value的值是数组,所以可以将多个url映射到同一个方法上。
窄化请求映射
在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理。如下:@RequestMapping放在类名上边,设置请求前缀。
@Controller
@RequestMapping("/item")
public class ItemController {
...
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
然后在方法名上边设置请求映射url,即@RequestMapping注解应放在方法名上边,如下:
@RequestMapping("/itemList")
public ModelAndView getItemsList() {
// 查询商品列表
List<Items> itemList = itemService.getItemList();
// 把查询结果传递给页面
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList); // addObject方法相当于放到request域上
// 设置逻辑视图
modelAndView.setViewName("itemList");
// 返回结果
return modelAndView;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
最后在浏览器中输入地址进行访问时,访问地址应为:http://localhost:8080/springmvc-web2/item/itemList.action。
请求方法限定
限定GET方法:@RequestMapping(method = RequestMethod.GET)。
如果通过post方式访问则报错:
以例明示:@RequestMapping(value="/updateitem",method={RequestMethod.GET})- 1
限定POST方法:@RequestMapping(method = RequestMethod.POST)。
如果通过get方式访问则报错:
以例明示:@RequestMapping(value="/updateitem",method={RequestMethod.POST})- 1
GET和POST都可以:@RequestMapping(method={RequestMethod.GET,RequestMethod.POST})。
以例明示:@RequestMapping(value="/updateitem",method={RequestMethod.POST,RequestMethod.GET})- 1
注意,这儿可能有中文乱码问题,大家须谨慎对待。
SpringMVC学习(七)——Controller类的方法返回值
本文所有案例代码的编写均建立在前文SpringMVC学习(六)——SpringMVC高级参数绑定与@RequestMapping注解的案例基础之上,因此希望读者能仔细阅读这篇文章。
返回ModelAndView
Controller类方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。之前我就已讲过,在此并不过多赘述。
返回void
在Controller类方法形参上可以定义request和response,使用request或response指定响应结果:
使用request转向页面,如下:
request.getRequestDispatcher("页面路径").forward(request, response);- 1
之前我们实现商品列表的查询,返回的是ModelAndView,如果现在该方法的返回值是void,那么就应使用request跳转页面,如下:
@RequestMapping("/itemList2") public void itmeList2(HttpServletRequest request, HttpServletResponse response) throws Exception { // 查询商品列表 List<Items> itemList = itemService.getItemList(); // 向页面传递参数 request.setAttribute("itemList", itemList); // 如果使用原始的方式做页面跳转,必须给的是jsp的完整路径 request.getRequestDispatcher("/WEB-INF/jsp/itemList.jsp").forward(request, response); }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
注意:如果使用原始的方式做页面跳转,那么必须给定jsp页面的完整路径。
也可以通过response实现页面重定向:
response.sendRedirect("url")- 1
也可以通过response指定响应结果,例如响应json数据如下:
response.setCharacterEncoding("utf-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().write("json串");- 1
- 2
- 3
例如,将以上itmeList2方法修改为:
@RequestMapping("/itemList2") public void itmeList2(HttpServletRequest request, HttpServletResponse response) throws Exception { PrintWriter writer = response.getWriter(); response.setCharacterEncoding("utf-8"); response.setContentType("application/json;charset=utf-8"); writer.write("{\"id\":\"123\"}"); }- 1
- 2
- 3
- 4
- 5
- 6
- 7
此时,在浏览器地址栏中输入url访问地址:
http://localhost:8080/springmvc-web2/item/itemList2.action进行访问,我们可在浏览器看到如下效果:
返回字符串
逻辑视图名
Controller类方法返回字符串可以指定逻辑视图名,通过视图解析器解析为物理视图地址。
Redirect重定向
Contrller类方法返回结果重定向到一个url地址,如下商品信息修改提交后重定向到商品查询方法,参数无法直接带到商品查询方法中。
@RequestMapping(value="/updateitem",method={RequestMethod.POST,RequestMethod.GET})
public String updateItems(Items items) {
itemService.updateItem(items);
// '/'是不包含工程名的根目录,即http://localhost:8080/springmvc-web2/item/itemList.action
return "redirect:/item/itemList.action";
}
- 1
- 2
- 3
- 4
- 5
- 6
redirect方式相当于“response.sendRedirect()”,转发后浏览器的地址栏变为转发后的地址,因为转发即执行了一个新的request和response。由于新发起一个request,原来的参数在转发时就不能传递到下一个url,如果要传参数可以在/item/itemList.action后边加参数,如下:
return "redirect:/item/itemList.action?id=xxx&name=xxx";
- 1
但如果你使用的是Model接口,那么SpringMVC框架会自动将Model中的数据拼装到/item/itemList.action后面。
forward转发
Controller类方法执行后继续执行另一个Controller类方法,如下商品修改提交后转向到商品修改页面,修改商品的id参数可以直接带到商品修改方法中。
@RequestMapping(value="/updateitem",method={RequestMethod.POST,RequestMethod.GET})
public String updateItems(Items items) throws UnsupportedEncodingException {
itemService.updateItem(items);
return "forward:/item/itemList.action";
}
- 1
- 2
- 3
- 4
- 5
forward方式相当于“request.getRequestDispatcher().forward(request,response)”,转发后浏览器地址栏还是原来的地址。转发并没有执行新的request和response,而是和转发前的请求共用一个request和response。所以转发前请求的参数在转发后仍然可以读取到。
SpringMVC学习(八)——SpringMVC中的异常处理器
SpringMVC在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑。
异常处理思路
我们知道,系统中异常包括两类:预期异常和运行时异常(RuntimeException),前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。系统的dao、service、controller出现异常都通过throws Exception向上抛出,最后由SpringMVC前端控制器交由异常处理器进行异常处理,SpringMVC提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。如下图:
明白了SpringMVC中的异常处理机制,下面就开始分析SpringMVC中的异常处理。
全局异常处理器案例
自定义异常类
为了区别不同的异常通常根据异常类型自定义异常类,这里我们创建一个自定义系统异常,如果controller、service、dao抛出此类异常说明是系统预期处理的异常信息。
我们可在工程的com.itheima.springmvc.exception包下编写一个自定义异常类——CustomerException.java,如下:
public class CustomerException extends Exception {
private String expMessage;
public CustomerException() {
}
public CustomerException(String msg) {
this.expMessage = msg;
}
public String getExpMessage() {
return expMessage;
}
public void setExpMessage(String expMessage) {
this.expMessage = expMessage;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
自定义异常处理器
全局异常处理器处理思路:
- 解析出异常类型。
- 如果该异常类型是系统自定义的异常,直接取出异常信息,在错误页面展示。
- 如果该异常类型不是系统自定义的异常,那么应取出错误的堆栈信息,并记录下来,在错误页面展示。
SpringMVC提供一个HandlerExceptionResolver接口,自定义全局异常处理器必须要实现这个接口,所以我们可在com.itheima.springmvc.exception包下编写一个自定义全局异常处理器,如下:
/**
* 全局异常处理器
* @author 李阿昀
*
*/
public class GlobalExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception exception) {
// 判断异常的种类
String msg = null;
if (exception instanceof CustomerException) {
// 如果是自定义异常,就从异常里面取出错误消息
CustomerException custExp = (CustomerException) exception;
msg = custExp.getExpMessage();
} else {
// 如果是运行时异常,则取错误的堆栈信息
exception.printStackTrace(); // 向控制台上打印堆栈信息
StringWriter s = new StringWriter();
PrintWriter printWriter = new PrintWriter(s);
exception.printStackTrace(printWriter);
msg = s.toString();
}
// 写日志、发短信、发邮件
// 在此省略这一步......
// 返回一个友好的错误页面,并显示错误消息
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("msg", msg);
modelAndView.setViewName("error");
return modelAndView;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
记得还要在/WEB-INF/jsp目录下提供一个错误页面——error.jsp,如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<h1>系统发送异常了。。。。。。。。。。。。</h1>
<br/>
<h2>错误消息为:</h2>
<br/>
${msg }
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
异常处理器配置
全局异常处理器中的逻辑很清楚,我就不再多说了,然后就是在springmvc.xml文件中配置这个自定义的异常处理器:
<!-- 配置全局异常处理器,且全局异常处理器只能有一个 -->
<bean class="com.itheima.springmvc.exception.GlobalExceptionResolver"></bean>
- 1
- 2
测试
接下来就是要写测试程序了,先将ItemController类中的getItemsList方法改造为:
@RequestMapping("/itemList")
public ModelAndView getItemsList() throws Exception {
// 自定义异常测试
if (true) {
throw new CustomerException("这是我们自定义的异常,哈哈哈。。。。。。。。。");
}
// 查询商品列表
List<Items> itemList = itemService.getItemList();
// 把查询结果传递给页面
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList); // addObject方法相当于放到request域上
// 设置逻辑视图
modelAndView.setViewName("itemList");
// 返回结果
return modelAndView;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
然后我们在浏览器中输入url来测试:http://localhost:8080/springmvc-web2/item/itemList.action。getItemsList方法中无论如何都会抛出一个自定义的异常,然后被上面配置的全局异常处理器捕获并执行,跳转到我们指定的页面,如下:
接着再将ItemController类中的getItemsList方法改造为:
@RequestMapping("/itemList")
public ModelAndView getItemsList() throws Exception {
// 运行时异常的测试
int i = 1/0;
// 查询商品列表
List<Items> itemList = itemService.getItemList();
// 把查询结果传递给页面
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("itemList", itemList); // addObject方法相当于放到request域上
// 设置逻辑视图
modelAndView.setViewName("itemList");
// 返回结果
return modelAndView;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
然后我们在浏览器中输入url来测试:http://localhost:8080/springmvc-web2/item/itemList.action。getItemsList方法中这时就会抛出一个运行时异常,然后被上面配置的全局异常处理器捕获并执行,跳转到我们指定的页面,如下:
可以看出在自定义的异常处理器中能获取导致出现异常的对象,有利于提供更详细的异常处理信息。一般用这种自定义的全局异常处理器比较多。关于SpringMVC的异常处理,就总结这么多吧。
SpringMVC学习(九)——SpringMVC中实现文件上传
这一篇博文主要来总结下SpringMVC中实现文件上传的步骤。但这里我只讲单个文件的上传。
环境准备
SpringMVC上传文件的功能需要两个jar包的支持,如下:
工程中肯定要导入以上两个jar包,主要是CommonsMultipartResolver解析器依赖commons-fileupload和commons-io这两个jar包。
单个文件的上传
前台页面
我们要改造editItem.jsp页面,主要是在form表单中添加商品图片一栏,效果我截图如下:
注意一点的是form表单中别忘了写enctype="multipart/form-data"属性。
对多部件类型multipart解析
意思就是说针对上面的enctype=”multipart/form-data”类型,SpringMVC需要对multipart类型的数据进行解析,在springmvc.xml中配置multipart类型解析器即可。如下:
<!-- 配置多媒体文件解析器 -->
<!-- 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为5MB -->
<property name="maxUploadSize">
<value>5242880</value>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
创建文件保存的虚拟目录
在上传文件之前,首先要创建一个虚拟目录来保存文件,这个虚拟目录会对应磁盘上的一个实际的目录,在实际开发中肯定会有一个服务器专门存储资源的,在这里我们就用本地磁盘来保存文件,然后映射一个虚拟目录,用来在程序中指定获取文件的路径(其实上面前台页面editItem.jsp中,那个src=”/pic/${item.pic}”中的/pic就是虚拟目录)。
创建的方法有两种:一是在Eclipse中双击tomcat服务器,就会弹出下面的框框:
选择Modules,然后点击【Add External Web Module…】,在弹出的对话框中进行如下操作,都在图里面了。
设置好后,保存即可,这样上传的文件都会保存到Document base指定的目录中,相当于虚拟映射到Path指定的目录中,程序中获取这个文件,要从Path指定的虚拟目录中获取,即我上面的/pic。我可举一个例子,将一个图片(例如,0d318effd664668f555d43966b3294a6_b.jpg)拷贝到Document base指定的目录中,即F:\temp\images目录中,然后启动tomcat服务器,在浏览器地址栏中输入url访问地址——http://localhost:8080/pic/0d318effd664668f555d43966b3294a6_b.jpg即可访问到该图片。
第二种方法就是在tomcat的配置文件中配置一下,其实刚刚在Eclipse中的操作已经自动写到这个配置文件中了,配置文件位置在tomcat目录/conf/server.xml中,看一下里面会多了一行:
这就是刚刚我配置的,它自动写到这个文件中了,所以我们也可以直接自己在该配置文件中写,就不需要在Eclipse中配置了。
编写后台Controller方法
接下来就是重点了,前台传过来的文件,我们在ItemController类中需要进行处理,然后保存到磁盘中,同时也就映射到了我们配置的虚拟路径中了,那么如何接收呢?看下面的代码:
@RequestMapping(value="/updateitem",method={RequestMethod.POST,RequestMethod.GET})
public String updateItems(Items items, MultipartFile picture) throws Exception {
// 把图片保存到图片目录下
// 保存图片,这个图片有的时候文件名可能会重复,你保存多了会把原来的图片给覆盖掉,这就不太合适了。
// 所以为每个文件生成一个新的文件名
String picName = UUID.randomUUID().toString();
// 截取文件的扩展名(如.jpg)
String oriName = picture.getOriginalFilename();
String extName = oriName.substring(oriName.lastIndexOf("."));
// 保存文件
picture.transferTo(new File("F:\\temp\\images\\" + picName + extName));
// 把文件名保存到数据库
items.setPic(picName + extName);
itemService.updateItem(items);
return "forward:/item/itemEdit.action";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
注意:考虑实际情况,保存图片时一般不只你一个人保存图片,那么这个图片有的时候文件名可能会重复,你保存多了会把原来的图片给覆盖掉,这就不太合适了,所以需要使用UUID算法为每个文件生成一个新的文件名。
首先来看一下形参,主要有Items和MultipartFile类型的picture,我这里上传一张图片是ItemsCustom类的一个属性,所以有了这个形参,是为了写到该类中,SpringMVC文件上传的类是MultipartFile,参数名称picture必须和前台editItem.jsp页面的name属性一致才行。上传图片之后的效果类似于:
并且图片的文件名也保存到了数据库表中,如下:
最后总结一下,我上传文件的逻辑可能判断不严谨,如果说要更加严谨一点的话, 上传文件的逻辑就应是:首先判断有没有上传文件,如果上传了,那么对文件重新命名然后写到磁盘中。如果没有上传文件,那么我应该还是用原来的文件(图片)。读者如果有兴趣可以参考下面代码:
@RequestMapping(value="/updateitem",method={RequestMethod.POST,RequestMethod.GET})
public String updateItems(Items items, MultipartFile picture) throws Exception {
// 处理上传的单个图片
String originalFileName = picture.getOriginalFilename();// 原始名称
// 上传图片
if (picture != null && originalFileName != null && originalFileName.length() > 0) {
// 存储图片的物理路径,实际中是要写到配置文件中的,不能在这写死
String pic_path = "F:\\temp\\images\\";
// 新的图片名称
String newFileName = UUID.randomUUID()
+ originalFileName.substring(originalFileName
.lastIndexOf("."));
File newFile = new File(pic_path + newFileName);//新图片
picture.transferTo(newFile);// 将内存中的数据写入磁盘
items.setPic(newFileName);// 将新图片名称写到itemsCustom中
} else {
//如果用户没有选择图片就上传了,还用原来的图片
Items temp = itemsService.findItemsById(items.getId());
items.setPic(temp.getPic());
}
// 调用service更新商品信息,页面需要将商品信息传到此方法
itemService.updateItem(items);
return "forward:/item/itemEdit.action";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
到这里,单个文件的上传我就已总结完了。
json数据格式在接口调用中、html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍。在SpringMVC中,也支持对json数据的解析和转换,这篇文章主要总结一下在SpringMVC中如何和前台交互json数据。
两种交互形式
SpringMVC和前台交互主要有两种形式,如下图所示:
可以看出,前台传过来的方式有两种,一种是传json格式的数据过来,另一种就是在url的末尾传普通的key/value串过来,针对这两种方式,在Controller类中会有不同的解析,但是在Controller类中返回的json格式的数据都是一样的。下面来具体分析一下SpringMVC是如何与前台进行json数据的交互的。在讲之前先认识两个注解。
@RequestBody注解
@RequestBody注解用于读取http请求的内容(字符串),通过SpringMVC提供的HttpMessageConverter接口将读到的内容转换为json、xml等格式的数据并绑定到Controller类方法的参数上。
本例子应用:@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象。如下:
@ResponseBody注解
@ResponseBody注解用于将Controller类的方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据如:json、xml等,通过Response响应给客户端。
本例子应用:@ResponseBody注解实现将Controller类方法返回对象转换为json响应给客户端,如下:
经过我如此细致地讲解,想必大家已认识到这两个注解的意思了。好了,下面来具体分析一下SpringMVC是如何与前台进行json数据的交互的。
环境的准备
加载json的jar包
SpringMVC默认用MappingJacksonHttpMessageConverter对json数据进行转换,需要加入jackson的包,又因为SpringMVC3和SpringMVC4针对json交互的jar包有区别,我用的是SpringMVC4,需要导入如下三个jar包:
读者千万不要忘了导入jQuery的类库,因为我是使用jQuery的ajax提交json串的,就像下图这样:
配置json转换器
配置json转换器有两种方式,如果是配置了注解适配器org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter的话,需要在该适配器中配置json转换器,如下:
<!-- 用于将对象转换为 JSON -->
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
但是如果使用<mvc:annotation-driven />注解驱动的话就不用以上的配置了,默认已经配好了。建议使用这种,比较方便。
json交互的测试
这里,我使用jQuery的ajax提交json串,对输出的json结果进行解析。前台的程序如下:
<button onclick="sendJson()">json数据交互测试</button>
<script type="text/javascript">
function sendJson() {
$.ajax({
type:"post",
url:"${pageContext.request.contextPath }/item/json_test.action",
data:'{"id":"1","name":"电冰箱","price":"1999"}',
contentType:"application/json;charset=utf-8",
success:function(data) {
alert(data.id + ":" + data.name);
}
});
}
</script>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
那么前台itemList.jsp页面的内容就应改造为:
接着编辑ItemController类,并在该类中编写如下方法:
// JSON数据交互
// @RequestBody:接收json数据并转换成pojo对象
// @ResponseBody:响应json数据,把pojo对象转换成json数据并响应
@RequestMapping("/json_test")
@ResponseBody
public Items jsonTest(@RequestBody Items items) {
return items;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
由于前台传的是id、name和price三个属性,所以在后台就用Items类来接收了,这个类中也有这三个属性。重点是@RequestBody注解,它是将前台传过来的json串转换成items对象,然后再将该对象return回去,通过@ResponseBody注解将items对象转成json格式返回给前台。这样前台接收到了后就可以解析了。我们看一下测试的结果:
响应的结果,null表示空值而已,这里就返回了原对象,也就只有id、name和price属性。
springmvc与前台json数据的交互就总结这么多。
SpringMVC学习(十一)——SpringMVC实现Resultful服务
Restful就是一个资源定位及资源操作的风格,不是标准也不是协议,只是一种风格,是对http协议的诠释。
资源定位:互联网所有的事物都是资源,要求url中没有动词,只有名词,没有参数。url请求的风格就像这样:
http://blog.csdn.net/eson_15/article/details/51743514- 1
- 资源操作:使用put、delete、post、get等不同方法对资源进行操作,分别对应添加、删除、修改、查询。一般使用时还是post和get,put和delete几乎不使用。
现在有这样一个需求:使用RESTful方式实现商品信息查询。有需求,就要解决需求。我们可将ItemController类中的editItem方法改造为:
@RequestMapping("/itemEdit/{id}")
// 如果id和方法的形参一致,@PathVariable注解中可以不写内容
public String editItem(@PathVariable("id") Integer iid, Model model) {
// 调用服务
Items items = itemService.getItemById(iid);
// 把数据传递给页面,需要用到Model接口
model.addAttribute("item", items);
// 返回逻辑视图
return "editItem";
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
@RequestMapping(value="/itemEdit/{id}"):{×××}表示占位符,请求的URL可以是“/itemEdit/1”或“/itemEdit/2”,通过在方法中使用@PathVariable获取{×××}中的×××变量。@PathVariable用于将请求URL中的模板变量映射到功能处理方法的参数上。如果@RequestMapping中表示为”/viewItems/{id}”,id和形参名称一致,那么@PathVariable就不用指定名称。
除此之外,还要在前端控制器中针对REST进行配置,即将web.xml文件中的前端控制器配置改造为:
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 指定springmvc配置文件的路径。如果不指定,默认为:/WEB-INF/${servlet-name}-servlet.xml -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /:拦截所有请求,不包括jsp。/*:拦截所有请求,包括jsp,应该配置"/" -->
<url-pattern>/</url-pattern>
</servlet-mapping>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
在SpingMVC框架中,/即表示拦截所有请求,但不拦截jsp,/*即表示所有的都拦截,包括jsp。这里显然应该配置/,因为你想啊!假如说Controller类方法里面已经把数据处理完了,要跳转到jsp页面,如果你配置的是/*,这个jsp页面的跳转也会被拦截,就会报404错误。
为了便于测试,将itemList.jsp中的修改超链接改造为:
<td><a href="${pageContext.request.contextPath }/item/itemEdit/${item.id}">修改</a></td>
- 1
最后的一个测试效果为:
但是有个问题,使用上面的配置后会拦截所有的url(虽说不包括jsp),那么对静态资源也会拦截,所以DispatcherServlet也会解析静态资源,但是这样的话就会出错,所以我们要设置一下不让它解析静态资源。SpringMVC的<mvc:resources mapping="" location="">标签可实现对静态资源进行映射访问。如下是对js文件的访问配置:
<mvc:resources location="/js/" mapping="/js/**"/>
- 1
如果说我们把js文件夹放到工程的WEB-INF目录下,照理来说外界肯定是无法访问到的,但是我们非得访问呢?这时就可通过<mvc:resources mapping="" location="">标签来对它进行映射访问了,如下:
SpringMVC学习(十二)——SpringMVC中的拦截器
SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。本文主要总结一下SpringMVC中拦截器是如何定义的,以及测试拦截器的执行情况和使用方法。
SpringMVC中拦截器的定义和配置
SpringMVC中拦截器的定义
在SpringMVC中,定义拦截器要实现HandlerInterceptor接口,并实现该接口中提供的三个方法,如下:
public class Interceptor1 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("Interceptor1 preHandle........");
// 执行的时机是在Handler执行之前执行此方法
// 返回值:如果返回true,就放行,不拦截,正常执行Handler进行处理
// 返回值:如果返回false,那就拦截,Handler就不能正常处理了
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// Handler执行之后,在返回ModelAndView之前,对modelAndView做些手脚
System.out.println("Interceptor1 postHandle........");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// 返回ModelAndView之后
// Handler执行过程中出现异常,可以在此处理异常
System.out.println("Interceptor1 afterCompletion........");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
针对这三个方法,我做一下简单的分析:
- preHandle方法:该方法执行的时机是在Handler执行之前执行。可以用于身份认证、身份授权等。比如如果认证没有通过表示用户没有登录,需要此方法拦截不再往下执行(return false),否则就放行(return true)。
- postHandle方法:该方法执行的时机是在Handler执行之后,在返回ModelAndView之前执行,可以看到该方法中有个modelAndView的形参。应用场景:从modelAndView出发,将公用的模型数据(比如菜单导航之类的)在这里传到视图,也可以在这里统一指定视图。
- afterCompletion方法:返回ModelAndView之后执行。应用场景:统一异常处理(即Handler执行过程中出现异常,可以在此处理异常),统一日志处理等。
SpringMVC中拦截器的配置
针对某种HandlerMapping配置拦截器
在SpringMVC中,拦截器是针对具体的HandlerMapping进行配置的,也就是说如果在某个HandlerMapping中配置拦截,经过该HandlerMapping映射成功的Handler最终会使用该拦截器。比如,假设我们在springmvc.xml配置文件中配置的映射器是org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,那么我们可以这样来配置拦截器:
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="handlerInterceptor1"/>
<ref bean="handlerInterceptor2"/>
</list>
</property>
</bean>
<bean id="handlerInterceptor1" class="com.itheima.springmvc.interceptor.HandlerInterceptor1"/>
<bean id="handlerInterceptor2" class="com.itheima.springmvc.interceptor.HandlerInterceptor2"/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
针对所有HandlerMapping配置全局拦截器
那么在SpringMVC中,如何配置类似于全局的拦截器呢?上面也说了,SpringMVC中的拦截器是针对具体的映射器而言的,为了解决这个问题,SpringMVC框架将配置的类似全局的拦截器注入到每个HandlerMapping中,这样就可以成为全局的拦截器了。配置如下:
<!-- 配置拦截器 -->
<mvc:interceptors>
<!-- 按照配置的顺序执行拦截器 -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.itheima.springmvc.interceptor.Interceptor1"></bean>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.itheima.springmvc.interceptor.Interceptor2"></bean>
</mvc:interceptor>
</mvc:interceptors>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
注意:path=”/**”表示拦截所有的url包括子url路径。在实际开发中,一般我们都用这种配置,<mvc:mapping>中指定要拦截的url即可。
SpringMVC拦截器的执行测试
仿照上面的HandlerInterceptor1再写两个拦截器,HandlerInterceptor2和HandlerInterceptor3,配置是按照上面这个配置,然后我们来测试一下三个拦截器的执行情况,并做相关总结。
三个拦截器都放行
也就是说,我们将三个拦截器的preHandle方法中的返回值都改成true,来测试一下拦截器的执行顺序,访问任何一个url,测试结果都将如下:
Interceptor1 preHandle........
Interceptor2 preHandle........
Interceptor3 preHandle........
Interceptor3 postHandle........
Interceptor2 postHandle........
Interceptor1 postHandle........
Interceptor3 afterCompletion........
Interceptor2 afterCompletion........
Interceptor1 afterCompletion........
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
根据打印的结果做一个总结:当所有拦截器都放行的时候,preHandle方法是按照配置的顺序执行的;而另外两个方法按照配置的顺序逆向执行。
有一个拦截器不放行
我们将第三个拦截器的preHandle方法中的返回值改成false,前两个还是true,来测试一下拦截器的执行顺序,访问任何一个url,测试结果都将如下:
Interceptor1 preHandle........
Interceptor2 preHandle........
Interceptor3 preHandle........
Interceptor2 afterCompletion........
Interceptor1 afterCompletion........
- 1
- 2
- 3
- 4
- 5
根据打印的结果做一个总结:
- 由于拦截器1和2放行,所以拦截器3的preHandle才能执行。也就是说前面的拦截器放行了,后面的拦截器才能执行preHandle方法。
- 拦截器3不放行,所以其另外两个方法没有被执行。即如果某个拦截器不放行,那么它的另外两个方法就不会被执行。
- 只要有一个拦截器不放行,所有拦截器的postHandle方法都不会执行,但是只要执行过preHandle并且放行的,就会执行afterCompletion方法。
三个拦截器都不放行
这种情况其实可以参考上面的情况了,是一个特例,也看一下运行结果:
Interceptor1 preHandle........
- 1
很明显,就只执行了第一个拦截器的preHandle方法,因为都不放行,所以没有一个执行postHandle方法和afterCompletion方法。
总结
- preHandle按拦截器定义顺序调用
- postHandler按拦截器定义逆序调用
- afterCompletion按拦截器定义逆序调用
- postHandler在拦截器链内所有拦截器返回true才调用
- afterCompletion只有preHandle返回true才调用
拦截器的应用
从第二种情况来看,比如现在要写一个统一异常处理的逻辑,那么要将该拦截器放在拦截器链的第一个位置,且一定要放行,因为只有放行了,才会去执行afterCompletion,而且放在拦截器链的第一个的话,afterCompletion方法会最后执行,才能在里面执行统一异常处理的逻辑。
再比如,登录认证拦截器,要放在拦截器链接中的第一个位置(如果有统一异常处理,那么应该放在统一异常处理的后面);权限校验拦截器,放在登录认证拦截器之后(因为登录通过后才校验权限)。但我在这里只写一个登录验证的拦截器来以示说明如何使用SpringMVC的拦截器。
处理流程
- 先得有一个登录页面,然后需要写一个Controller类访问该页面
- 登录页面有一个提交表单的动作,也即需要在Controller类中进行处理
- a) 判断用户名密码是否正确
- b) 如果正确,向Session中写入用户信息
- c) 返回登录成功页面,或者跳转到商品列表页面
- 拦截器的处理
- a) 拦截用户请求,判断用户是否登录
- b) 如果用户已经登录,则放行
- c) 如果用户未登录,则跳转到登录页面
提供一个登录页面
在/WEB-INF/jsp目录下编写一个login.jsp登录页面,如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/login" method="post">
<label>用户名</label>
<input type="text" name="username" />
<br/>
<label>密码</label>
<input type="password" name="password" />
<br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
编写实现用户登录的Controller
在com.itheima.springmvc.controller包下编写实现用户登录的UserController,如下:
public class UserController {
@RequestMapping("/user/showlogin")
public String showLogin() {
return "login";
}
@RequestMapping("/user/login")
public String userLogin(String username, String password, HttpSession session) {
// a) 判断用户名密码是否正确
System.out.println(username);
System.out.println(password);
// b) 如果正确,向session中写入用户信息
session.setAttribute("username", username);
// c) 返回登录成功页面,或者跳转到商品列表页面
return "redirect:/item/itemList.action";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
登录验证拦截器的实现
在com.itheima.springmvc.interceptor包下编写一个登录验证的拦截器——LoginInterceptor.java,如下:
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
String uri = request.getRequestURL().toString();
// 如果用户未登录,则跳转到登录页面,跳转的过程中有可能被拦截,所以得做一个判断
if (uri.contains("login")) {
return true;
}
System.out.println("Interceptor1 preHandle........");
// a) 拦截用户请求,判断用户是否登录
HttpSession session = request.getSession();
Object username = session.getAttribute("username");
if (username != null) {
// b) 如果用户已经登录。放行
return true;
}
// c) 如果用户未登录,跳转到登录页面,跳转的过程中有可能被拦截,所以得做一个判断
response.sendRedirect(request.getContextPath() + "/user/showlogin");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// Handler执行之后,在返回ModelAndView之前,对modelAndView做些手脚
System.out.println("Interceptor1 postHandle........");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// 返回ModelAndView之后
// Handler执行过程中出现异常,可以在此处理异常
System.out.println("Interceptor1 afterCompletion........");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
然后配置该拦截器:
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.itheima.springmvc.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
这样当我们任意请求一个url的时候,就会被刚刚我们定义的拦截器给捕获到,然后会判断session中是否有用户信息,没有的话就会跳到登陆页面让我们登陆。
SpringMVC入门与参数绑定
908

被折叠的 条评论
为什么被折叠?



