//todo
5,spring MVC的本地化解析,文件上传,静态资源处理,拦截器,异常处理等
spring MVC 默认使用AcceptHeaderLocalResolver,根据报文头的Accept-Language参数来确定本地化类型;
此外还可以使用CookieLocalResolver和SessionLocalResolver,如果想从每次请求的参数中获取本次请求对应的本地化类型,采用LocalChangerIntercepter拦截器;
有空写个实现国际化的实例...
spring实现文件上传相当简单,首先引入依赖包,common-upload ,common-io包,然后定义个MultipartResolver解析器,写个表单就可实现;这里给个例子:
<dependency> <groupId>commons-fileuploadgroupId> <artifactId>commons-fileuploadartifactId> <version>1.3version> dependency>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/> <property name="maxUploadSize" value="2097152"/> <property name="uploadTempDir" value="temp"/> bean>
<form action="/upload" enctype="multipart/form-data" method="post"> <input type="file" name="file"/> <input type="submit"> form>
@RequestMapping(value = "/upload") public String uploadImg(@RequestParam("file")MultipartFile file){ if(null!=file) { File newFile=new File("E://aaa.jpg"); try { file.transferTo(newFile); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } return "redirect:/account/list"; }
结果是在E盘看到了一个上传的aaa.jpg图片;
静态资源的处理:在spring mvc的配置文件当中加上如下的配置,同时用默认的资源处理器和spring mvc自带的资源处置方式;
还可以在一个类中增加版本号的形式改变mapping的名称;
装配拦截器:
package com.lfc.sh.web.sp.framework.intercepter; import org.apache.log4j.Logger; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Company: Copyright© 2009 www.7road.com All rights reserved. * com.lfc.sh.web.sp.framework.intercepter * Create Date: 13-11-19 下午5:47 * Note: 登录的拦截器 */ public class LoginIntercepter implements HandlerInterceptor { private static Logger logger = Logger.getLogger(LoginIntercepter.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { logger.warn("进入登录拦截器的preHandle方法"+" url:"+ request.getRequestURI()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { logger.warn("进入登录拦截器的postHandle方法"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { logger.warn("进入登录拦截器的afterCompletion方法"); } }
下面是日志:
2013-11-19 18:00:16 com.lfc.sh.web.sp.framework.intercepter.LoginIntercepter.preHandle(26)---[160013]
[WARN] 进入登录拦截器的preHandle方法 url:/account/create
2013-11-19 18:00:16 com.lfc.sh.web.sp.framework.intercepter.LoginIntercepter.postHandle(32)---[160015]
[WARN] 进入登录拦截器的postHandle方法
2013-11-19 18:00:16 com.lfc.sh.web.sp.framework.intercepter.LoginIntercepter.afterCompletion(37)---[160028]
[WARN] 进入登录拦截器的afterCompletion方法
异常处理:
spring Mvc 通过HandlerExceptionResolver处理程序的异常;
接口的功能见截图:
实现类:
spring mvc 默认安装的DefaultHandlerExceptionResolver,将异常转换为相应的状态码;
对某个Contoller指定异常处理@ExceptionHandler(Exception.class) @ResponseStatus
如果需要对所有的异常进行统一处理,可以通过SimpleMappingExceptionResolver来指定;
如果要自定,实现ExceptionResolve;
6,小结
spring mvc是一架实际精密的机器,理解其内部实现原理可以更好的使用和扩展它;