2 注解开发-高级
2.1 上传图片
2.1.1 配置虚拟目录
2.1.2 配置解析器
[html] view plain copy print?
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize">
<value>5242880value>
property>
bean>
2.1.3 jar包
CommonsMultipartResolver解析器依赖commons-fileupload和commons-io,加入如下jar包:
2.1.4 单个图片上传
1、controller:
[java] view plain copy print?
//商品修改提交
@RequestMapping("/editItemSubmit")
public String editItemSubmit(Items items, MultipartFile pictureFile)throws Exception{
//原始文件名称
String pictureFile_name = pictureFile.getOriginalFilename();
//新文件名称
String newFileName = UUID.randomUUID().toString()+pictureFile_name.substring(pictureFile_name.lastIndexOf("."));
//上传图片
File uploadPic = new java.io.File("F:/develop/upload/temp/"+newFileName);
if(!uploadPic.exists()){
uploadPic.mkdirs();
}
//向磁盘写文件
pictureFile.transferTo(uploadPic);
.....
2、 页面:
form添加enctype="multipart/form-data":
[html] view plain copy print?
<form id="itemForm"
action="${pageContext.request.contextPath }/item/editItemSubmit.action"
method="post" enctype="multipart/form-data">
<input type="hidden" name="pic" value="${item.pic }" />
file的name与controller形参一致:
[html] view plain copy print?
<tr>
<td>商品图片td>
<td><c:if test="${item.pic !=null}">
<img src="/pic/${item.pic}" width=100 height=100 />
<br />
c:if> <input type="file" name="pictureFile" />td>
tr>
2.2 Validation(了解)
b/s系统中对http请求数据的校验多数在客户端进行,这也是出于简单及用户体验性上考虑,但是在一些安全性要求高的系统中服务端校验是不可缺少的,本节主要学习springmvc实现控制层添加校验。
Spring3支持JSR-303验证框架,JSR-303是Java EE 6中的一项子规范,叫做Bean Validation,官方参考实现是hibernate Validator(与Hibernate ORM没有关系),JSR 303用于对Java Bean中的字段的值进行验证。
2.2.1 需求
对商品信息进行校验,是否必须,输入数据合法性。
2.2.2 加入jar包
2.2.3 配置validator
[html] view plain copy print?
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:CustomValidationMessagesvalue>
list>
property>
<property name="fileEncodings" value="utf-8" />
<property name="cacheSeconds" value="120" />
bean>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
<property name="validationMessageSource" ref="messageSource" />
bean>
2.2.4 将validator加到处理器适配器
配置方式1:
[html] view plain copy print?
<bean id="customBinder"
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="validator" ref="validator" />
bean>
[html] view plain copy print?
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer" ref="customBinder">property>
bean>
配置方式2:
[html] view plain copy print?
<mvc:annotation-driven validator="validator"> mvc:annotation-driven>
2.2.5 添加验证规则
[html] view plain copy print?
public class Items {
private Integer id;
@Size(min=1,max=30,message="{item.name.length.illigel}")
private String name;
@NotEmpty(message="{pic.is.null}")
private String pic;
2.2.6 错误消息文件CustomValidationMessages
如果在eclipse中编辑properties文件无法看到中文则参考“Eclipse开发环境配置-indigo.docx”添加propedit插件。
2.2.7 捕获错误
修改Controller方法:
[html] view plain copy print?
// 商品修改提交
@RequestMapping("/editItemSubmit")
public String editItemSubmit(@Validated @ModelAttribute("item") Items items,BindingResult result,
@RequestParam("pictureFile") MultipartFile[] pictureFile,Model model)
throws Exception {
//如果存在校验错误则转到商品修改页面
if (result.hasErrors()) {
List<ObjectError> errors = result.getAllErrors();
for(ObjectError objectError:errors){
System.out.println(objectError.getCode());
System.out.println(objectError.getDefaultMessage());
}
return "item/editItem";
}
注意:添加@Validated表示在对items参数绑定时进行校验,校验信息写入BindingResult中,在要校验的pojo后边添加BingdingResult, 一个BindingResult对应一个pojo,且BingdingResult放在pojo的后边。
商品修改页面:
页头:
[html] view plain copy print?
<%@ 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"
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
在需要显示错误信息地方:
[html] view plain copy print?
<spring:hasBindErrors name="item">
<c:forEach items="${errors.allErrors}" var="error">
${error.defaultMessage }<br/>
c:forEach>
spring:hasBindErrors>
说明:
2.2.8 分组校验
如果两处校验使用同一个Items类则可以设定校验分组。
定义分组:
分组就是一个标识,这里定义一个接口:
[html] view plain copy print?
public interface ValidGroup1 {
}
public interface ValidGroup2 {
}
指定分组校验:
[html] view plain copy print?
public class Items {
private Integer id;
//这里指定分组ValidGroup1,此@Size校验只适用ValidGroup1校验
@Size(min=1,max=30,message="{item.name.length.illigel}",groups={ValidGroup1.class})
private String name;
// 商品修改提交
@RequestMapping("/editItemSubmit")
public String editItemSubmit(@Validated(value={ValidGroup1.class}) @ModelAttribute("item") Items items,BindingResult result,
@RequestParam("pictureFile") MultipartFile[] pictureFile,Model model)
throws Exception {
在@Validated中添加value={ValidGroup1.class}表示商品修改使用了ValidGroup1分组校验规则,也可以指定多个分组中间用逗号分隔,
@Validated(value={ValidGroup1.class,ValidGroup2.class })
2.3 异常处理器
springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑。
2.3.1 异常处理思路
系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。
系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:
2.3.2 自定义异常类
为了区别不同的异常通常根据异常类型自定义异常类,这里我们创建一个自定义系统异常,如果controller、service、dao抛出此类异常说明是系统预期处理的异常信息。
[html] view plain copy print?
public class CustomException extends Exception {
/** serialVersionUID*/
private static final long serialVersionUID = -5212079010855161498L;
public CustomException(String message){
super(message);
this.message = message;
}
//异常信息
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
2.3.3 自定义异常处理器
[html] view plain copy print?
public class CustomExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
ex.printStackTrace();
CustomException customException = null;
//如果抛出的是系统自定义异常则直接转换
if(ex instanceof CustomException){
customException = (CustomException)ex;
}else{
//如果抛出的不是系统自定义异常则重新构造一个未知错误异常。
customException = new CustomException("未知错误,请与系统管理 员联系!");
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", customException.getMessage());
modelAndView.setViewName("error");
return modelAndView;
}
}
2.3.4 错误页面
[html] view plain copy print?
<%@ 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"%>
>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错误页面title>
head>
<body>
您的操作出现错误如下:<br/>
${message }
body>
html>
2.3.5 异常处理器配置
在springmvc.xml中添加:
[html] view plain copy print?
<bean id="handlerExceptionResolver" class="cn.itcast.ssm.controller.exceptionResolver.CustomExceptionResolver"/>
2.3.6 异常测试
修改商品信息,id输入错误提示商品信息不存在。
修改controller方法“editItem”,调用service查询商品信息,如果商品信息为空则抛出异常:
[html] view plain copy print?
// 调用service查询商品信息
Items item = itemService.findItemById(id);
if(item == null){
throw new CustomException("商品信息不存在!");
}
请自行实现在service、dao中跑出异常。
2.4 json数据交互
2.4.1 @RequestBody
作用:
@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容转换为json、xml等格式的数据并绑定到controller方法的参数上。
本例子应用:
@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象
2.4.2 @ResponseBody
作用:
该注解用于将Controller的方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过Response响应给客户端
本例子应用:
@ResponseBody注解实现将controller方法返回对象转换为json响应给客户端
2.4.3 请求json,响应json实现:
2.4.3.1 环境准备
Springmvc默认用MappingJacksonHttpMessageConverter对json数据进行转换,需要加入jackson的包,如下:
2.4.3.2 配置json转换器
在注解适配器中加入messageConverters
[html] view plain copy print?
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">bean>
list>
property>
bean>
注意:如果使用
2.4.3.3 controller编写
[html] view plain copy print?
// 商品修改提交json信息,响应json信息
@RequestMapping("/editItemSubmit_RequestJson")
public @ResponseBody Items editItemSubmit_RequestJson(@RequestBody Items items) throws Exception {
System.out.println(items);
//itemService.saveItem(items);
return items;
}
2.4.3.4 页面js方法编写:
引入 js:
[html] view plain copy print?
//请求json响应json
function request_json(){
$.ajax({
type:"post",
url:"${pageContext.request.contextPath }/item/editItemSubmit_RequestJson.action",
contentType:"application/json;charset=utf-8",
data:'{"name":"测试商品","price":99.9}',
success:function(data){
alert(data);
}
});
}
2.4.3.5 测试结果:
从上图可以看出请求的数据是json格式
2.4.4 Form提交,响应json实现:
采用form提交是最常用的作法,通常有post和get两种方法,响应json数据是为了方便客户端处理,实现如下:
2.4.4.1 环境准备
同第一个例子
2.4.4.2 controller编写
[html] view plain copy print?
// 商品修改提交,提交普通form表单数据,响应json
@RequestMapping("/editItemSubmit_ResponseJson")
public @ResponseBody Items editItemSubmit_ResponseJson(Items items) throws Exception {
System.out.println(items);
//itemService.saveItem(items);
return items;
}
2.4.4.3 页面js方法编写:
[html] view plain copy print?
function formsubmit(){
var user = " name=测试商品&price=99.9";
alert(user);
$.ajax(
{
type:'post',//这里改为get也可以正常执行
url:'${pageContext.request.contextPath}/item/ editItemSubmit_RequestJson.action',
//ContentType没指定将默认为:application/x-www-form-urlencoded
data:user,
success:function(data){
alert(data.name);
}
}
)
}
从上边的js代码看出,已去掉ContentType的定义,ContentType默认为:application/x-www-form-urlencoded格式。
2.4.4.4 测试结果:
从上图可以看出请求的数据是标准的key/value格式。
2.4.4.5 jquery的form插件插件
针对上边第二种方法,可以使用jquery的form插件提交form表单,实现ajax提交form表单,如下:
引用js:
js方法如下:
[html] view plain copy print?
function response_json() {
//form对象
var formObj = $("#itemForm");
//执行ajax提交
formObj.ajaxSubmit({
dataType : "json",//设置预期服务端返回json
success : function(responseText) {
alert(responseText);
}
});
}
2.4.5 小结
实际开发中常用第二种方法,请求key/value数据,响应json结果,方便客户端对结果进行解析。
2.5 RESTful支持
2.5.1 需求
RESTful方式商品修改、商品查询。
2.5.2 添加DispatcherServlet的rest配置
[html] view plain copy print?
<servlet>
<servlet-name>springmvc-servlet-restservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvc-servlet-restservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
2.5.3 URL 模板模式映射
@RequestMapping(value="/ editItem/{item_id}"):{×××}占位符,请求的URL可以是“/editItem/1”或“/editItem/2”,通过在方法中使用@PathVariable获取{×××}中的×××变量。
[html] view plain copy print?
@RequestMapping("/ editItem/{item_id}")
public String useredit(@PathVariable("item_id ") String id,Model model) throws Exception{
//方法中使用@PathVariable获取useried的值,使用model传回页面
model.addAttribute("userid", userid);
return"/user/useredit";
}
如果RequestMapping中表示为"/ editItem/{id}",id和形参名称一致,@PathVariable不用指定名称。
商品查询的controller方法也改为rest实现:
[html] view plain copy print?
// 查询商品列表
@RequestMapping("/queryItem")
public ModelAndView queryItem() throws Exception {
// 商品列表
List<Items> itemsList = itemService.findItemsList(null);
// 创建modelAndView准备填充数据、设置视图
ModelAndView modelAndView = new ModelAndView();
// 填充数据
modelAndView.addObject("itemsList", itemsList);
// 视图
modelAndView.setViewName("item/itemsList");
return modelAndView;
}
2.5.4 静态资源访问
spring mvc 的
如下是对js文件访问配置: