1.文件下载
用ResponseEntity<byte[]> 返回值完成文件下载
具体参见本博客之前的《文件上传下载》
@RequestMapping(value="/testResponseEntity")public ResponseEntity<byte[]> testResponseEntity(HttpServletRequest request) throws Exception{ ServletContext servletContext = request.getServletContext(); String fileName = "风吹麦浪.mp3"; String realPath = servletContext.getRealPath("/WEB-INF/"+fileName); InputStream in = new FileInputStream(new File(realPath)); byte[] body = new byte[in.available()]; in.read(body); MultiValueMap<String, String> headers = new HttpHeaders(); fileName = new String(fileName.getBytes("gbk"),"iso8859-1"); headers.set("Content-Disposition", "attachment;folename="+fileName); HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode); in.close(); return response;}
2.自定义**
Spring MVC也可以使用**对请求进行拦截处理,用户可以自定义**来实现特定的功能,自定义的**必 须实现HandlerInterceptor
接口 1).自定义实现类实现 HandlerInterceptor接口
public class MyInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("1-preHandle"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("1-postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("1-afterCompletion"); }}
三个方法
– preHandle():该方法在目标方法之前被调用, 若返回值为true,则继续调用后续的**和目标方法 若返回值为false,则不会再调用后续的**和目标方法 可以考虑做权限,日志或者事务等! – postHandle():调用目标方法之后,但渲染视图之前被调用 可以对请求域中的属性或者视图做出修改!
– afterCompletion():渲染视图之后被调用, 可以在该方法中进行一些资源清理的操作。
2).在springmvc配置文件中配置相对应的**
<mvc:interceptors> <!--配置自定义** ,MyFilter都拦截 --> <bean ></bean></mvc:interceptors>
若有多个**
<mvc:interceptors> <bean ></bean> <mvc:interceptor> <mvc:mapping path="/**"/><!-- 都拦截 --> <mvc:exclude-mapping path="/testInterceptor"/><!-- 除了这个都拦截 --> <bean ></bean> </mvc:interceptor></mvc:interceptors>
<mvc:mapping path=" "/> 与 <mvc:exclude-mapping path=" "/> 连用
前者代表:都拦截
后者代表:除了这个都拦截
3.SpringMVC的异常处理
1.加上<mvc:annotation-driven>标签: 2.在当前Handler中定义由@ExceptionHandler注解修饰的方法,用于处理异常信息! 注意: 1).@ExceptionHandler 方法修饰的入参中可以加入Exception类型的参数,该参数即对应发生的异常信息 2).@ExceptionHandler 方法的入参中不能传入Map.若希望把异常信息传到页面上,需要使用ModelAndView作为方法的返回值
@ExceptionHandler(value={ArithmeticException.class})public ModelAndView handleException(Exception ex){ ModelAndView mv = new ModelAndView(); mv.setViewName("error"); mv.addObject("exception", ex); return mv;}
3).@ExceptionHandler 注解定义的方法优先级问题: 例如发生的是NullPointerException,但是声明的异常有 RuntimeException 和 Exception,此候会根据异常的最近继承关系找到继承深度最浅的那个 @ExceptionHandler 注解方法,即标记了 RuntimeException 的方法 4).ExceptionHandlerMethodResolver 内部若找不到@ExceptionHandler 注解的话,会找@ControllerAdvice 中的@ExceptionHandler 注解方法
@ControllerAdvicepublic class HangleException { @ExceptionHandler(value={ArithmeticException.class}) public String handleException(){ System.out.println("no"); return "error"; }}
4,基于配置的异常处理
如果希望对所有异常进行统一处理,可以使用 SimpleMappingExceptionResolver,它将异常类名映射为视图名,即发生异常时使用对应的视图报告异常
<bean > <!-- exceptionAttribute用于指定异常对象在request域中的键 --> <property name="exceptionAttribute" value="ex"></property> <property name="exceptionMappings"> <props> <!-- key:指定异常的全类名 value:出现错误后要转发到的页面,但是此处没有value属性,所以直接写,如"error"--> <prop key="java.lang.ArithmeticException">error</prop> </props> </property></bean>
在 jsp页面写 ${exception },会在页面显示错误信息
这是因为默认将该信息写到 request 域中
如果不想用这个 名称,可以在配置信息中添加
<property name="exceptionAttribute" value="ex"></property>
这样在 jsp 页面写 ${ex } 也会在页面显示错误信息
原标题:SpringMVC(四)
关键词:Spring