Spring MVC异常处理 和 重定向传递数据
1.异常处理介绍
Spring在web项目中,如果在请求处理时出现异常,那输出会是Servlet响应。这时异常需要以某种方式转换为响应。
Spring将异常转换为响应的方式:
a.特定的Spring异常将自动映射为指定的HTTP状态码;
b.异常上添加@ResponseStatus注解,从而将其映射为某一个HTTP状态码;
c.方法上添加@ExceptionHandler注解,使其处理异常。
2.异常处理代码
@RequestMapping("getPathVariable/{id}")
public String getPathVariable(
@PathVariable("id") String id){
if("error".equals(id)){
throw new SpittleException();
}
return "index";
}
package com.taozhiye.controller; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(
//
value = HttpStatus.NOT_FOUND,
reason = "Spittle not found"
)
public class SpittleException extends RuntimeException { }
正常情况下,当id为error时,会报错,这时是500错误,我们可以通过@ResponseStatus注解,映射到404状态码上,进行简单的异常处理。
第二种方法是报相应的异常,直接跳转到错误页面。
package com.taozhiye.controller; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice
public class AppExcepitonHandler { @ExceptionHandler(Exception.class)
public String deal(){
System.out.println("出现异常");
return "index";
}
}
3.重定向传值
@RequestMapping("getPathVariable/{id}")
public String getPathVariable(
@PathVariable("id") String id,
Model model,
RedirectAttributes model2){
if("error".equals(id)){
throw new SpittleException();
}else if("findAll".equals(id)){
/**
* 重定向传参数:
* 相当于把参数写到session中,
* 如果重定向到controller的时候,用@ModelAttribute接收
* 如果重定向到页面,可以直接接收
*/
model2.addFlashAttribute("flash", "flash");
/**
* 通过url模板进行重定向
*/
return "redirect:/{id}";
}else if("index".equals(id)){
// model.addAttribute("id", id);
/**
* 转发不可以使用模板
*/
// return "/{id}";
model2.addFlashAttribute("flash", "flash");
/**
* 通过url模板进行重定向
*/
return "index";
}else{
return "ajax1";
}
}
@RequestMapping("/findAll")
@ResponseBody
public List<User> findAll(@ModelAttribute("flash") String flash){
System.out.println("flash:"+flash);
return userService.findUserAll();
}
@RequestMapping("/index")
@ResponseBody
public List<User> index(Map<String, Object> map,@ModelAttribute("flash")String flash){
System.out.println("flash:"+flash);
return userService.findUserAll();
}
重定向传参数:
相当于把参数写到session中,
如果重定向到controller的时候,用@ModelAttribute接收
如果重定向到页面,可以直接接收
Spring MVC异常处理 和 重定向传递数据的更多相关文章
- spring跨重定向传递数据
spring跨重定向传递数据 为何要重定向? 作用之一:防止表单重复提交 如何重定向? // 在控制器方法返回的视图名称中,以redirect:开头的String不是用来查找视图的,而是用来指导浏览器 ...
- Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装
阅读目录 1. 通过HttpServletRequest获得请求参数和数据 2. 处理方法形参名==请求参数名 3. 如果形参名跟请求参数名不一样怎么办呢?用@RequestParam注解 4. 用实 ...
- 0056 Spring MVC如何接收浏览器传递来的请求参数--request--形参--实体类封装
浏览器总会向服务器传递一些参数,那么Spring MVC如何接收这些参数? 先写个简单的html,向服务器传递一些书籍信息,如下: <!DOCTYPE html> <html> ...
- Spring MVC—数据绑定机制,数据转换,数据格式化配置,数据校验
Spring MVC数据绑定机制 数据转换 Spring MVC处理JSON 数据格式化配置使用 数据校验 数据校验 Spring MVC数据绑定机制 Spring MVC解析JSON格式的数据: 步 ...
- Spring MVC 3.0 返回JSON数据的方法
Spring MVC 3.0 返回JSON数据的方法1. 直接 PrintWriter 输出2. 使用 JSP 视图3. 使用Spring内置的支持// Spring MVC 配置<bean c ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- 【ASP.NET MVC系列】浅谈ASP.NET MVC 视图与控制器传递数据
ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...
- Spring MVC异常处理SimpleMappingExceptionResolver
Spring MVC异常处理SimpleMappingExceptionResolver[转] (2012-12-07 13:45:33) 转载▼ 标签: 杂谈 分类: 技术分享 Spring3.0中 ...
- Spring MVC异常处理代码完整实例
Spring MVC异常处理流程: 提供构造方法传值: 配置异常处理器的bean
随机推荐
- samba 配置文件解析
[global] #定义全局策略 workgroup=MYGROUP #定义工作组 netbios name=MYSERVER #指定NetBios名称 interfaces=lo 192.168.1 ...
- 5.html基础标签:块级+行级元素+特殊字符+嵌套规则
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CentOS防火墙配置
1.查询防火墙状态 service iptables status 2.开启防火墙 service iptables start 3.关闭防火墙 service iptables stop 4.重启防 ...
- HTML和CSS使用注意事项
HTML 1.button标签 在IE中,button标签默认的type是button,而在其他浏览器和W3C标准中button默认的属性都是submit. 所以,在一个form表单中,如果butto ...
- Android开发工程师文集-Android知识点讲解
前言 大家好,给大家带来Android开发工程师文集-Android知识点讲解的概述,希望你们喜欢 WebView讲解 一般通过Intent调用系统的浏览器: Uri uri = Uri.parse( ...
- Typescript 学习笔记六:接口
中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...
- 理解 Python 的执行方式,与字节码 bytecode 玩耍 (下)
上次写到,Python 的执行方式是把代码编译成bytecode(字节码)指令,然后由虚拟机来执行这些 bytecode 而 bytecode 长成这个样子: b'|\x00\x00d\x01\x0 ...
- 课程四(Convolutional Neural Networks),第一周(Foundations of Convolutional Neural Networks) —— 2.Programming assignments:Convolutional Model: step by step
Convolutional Neural Networks: Step by Step Welcome to Course 4's first assignment! In this assignme ...
- 关于微信JS SDK接口wx.previewImage预览接口的使用
然后后之前的项目,突然往微信上迁移了,一些微信的接口没怎么用过,比较陌生,这次的功能是想调用微信的接口,实现图片放大的功能, 就找到官方文档:http://qydev.weixin.qq.com/wi ...
- Log4Net使用详解1
log4net是一个功能著名的开源日志记录组件.利用log4net可以方便地将日志信息记录到文件.控制台.Windows事件日志和数据库(包括MS SQL Server, Access, Oracle ...