目录
视图解析器
什么是视图解析器
- SpringMVC视图解析器主要的作用是将 物理视图 转换成 逻辑视图
- 返回jsp return "/pages/list.jsp" 称为物理视图
- 把相同的前缀和后缀抽取出来 拼接在一起
配置视图解析器
- WebMvcConfigurer配置类其实是
Spring内部的一种配置方式 采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制
- 实现WebMvcConfigurer接口
- 重写configureViewResolvers方法
- registry.jsp(“前缀”,“后缀”);配置视图前缀和后缀
@ComponentScan(basePackages="com.czxy.mvc.controller")
public class MvcConfiguration implements WebMvcConfigurer {
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/pages/",".jsp");
Controller的使用
- 返回逻辑视图名
public class UserController {
@RequestMapping("/selectAll2")
public String selectAll2() {
绕过视图解析器
- 如果需要绕过视图解析器 有两种方法 使用请求转发或重定向
@RequestMapping("/user")
public class UserController {
* 最终访问路径 /user/selectAll.action
@RequestMapping("/selectAll")
public String selectAll() {
return "forward:/pages/list.jsp";
方法返回值
默认返回值ModelAndView
- Controller功能的方法 默认返回值是ModelAndView
@RequestMapping("/demo03")
public class Demo03BookController {
@RequestMapping("/list")
public ModelAndView list() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("book"); //之前 return "book";
//3 设置模型数据 key-value
modelAndView.addObject("username","jack");
return modelAndView;
返回字符串
- 返回字符串 有两种情况 一种是 请求转发 一种是 重定向
请求转发
return "forward:物理视图"; //jsp路径
重定向
return "redirect:物理视图"; //jsp路径
返回void
场景1:返回void 没有其他操作
- Controller返回void
@RequestMapping("/void1")
public void void1() {
System.out.println("void1 执行了 ");
场景2:请求调度器
- 使用servlet请求调度器完成 请求转发/请求包含 操作
@RequestMapping("/void2")
public void void2(HttpServletRequest request , HttpServletResponse response) throws Exception {
request.setAttribute("username","void2");
request.getRequestDispatcher("/WEB-INF/pages/book.jsp").forward(request, response);
//request.getRequestDispatcher("/WEB-INF/pages/book.jsp").include(request, response);
场景3:response响应数据
- response以流的方式 向浏览器响应数据
@RequestMapping("/void3")
public void void3(HttpServletRequest request , HttpServletResponse response) throws Exception {
response.getWriter().print("void3");
场景4:响应JSON
- 响应JSON数据的时候 必须加@ResponseBody注解 标明响应JSON数据
@RequestMapping("/void4")
public void void4() throws Exception {
System.out.println("哈哈");
异常处理器
什么是异常处理器
在Spring MVC中,提供了一个全局异常处理器,用于对系统中出现的异常进行统一的处理。在一般的系统中,DAO,Service及Controller层都出现的异常都以“throws Exception”的形式向上层抛出,最后都会由Spring MVC的前端控制器DispatcherServlet统一由全局异常处理器进行异常处理。
对于预期的异常,通常要定义一个自定义异常类,该类用于在发生异常后,存储异常信息,最终交给全局异常处理器处理该异常。
异常处理器执行流程
实现方式
- SpringMVC提供了异常处理器多种方式
- 方式一:实现HandlerExceptionResolver
- 方式二:通知类
使用@ControllerAdvice对Controller增强
使用@ExceptionHandler捕获异常
基本功能
需求
-
访问
/demo04/item.action?id=1-
id =1 系统异常
-
id =2 自定义异常
-
id =3 正常
-
步骤
- 编写自定义异常
- 编写Controller 接受一个参数 (系统异常 自定义异常 正常)
- 编写访问连接
实现
- 编写自定义异常 继承RuntimeException 重写构造
public class CustomExcption extends RuntimeException {
public CustomExcption() {
public CustomExcption(String message) {
public CustomExcption(String message, Throwable cause) {
super(message, cause);
public CustomExcption(Throwable cause) {
public CustomExcption(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
2.编写Controller 接受参数
@RequestMapping("/demo04")
public class Demo04ItemController {
@RequestMapping("/item")
public String item(Integer id ) {
int i = 1 / 0;
} else if( id == 2 ) {
throw new CustomExcption("自定义异常信息");
return "forward:/index.jsp";
3.编写访问连接
<a href="${pageContext.request.contextPath}/demo04/item.action?id=1">异常处理 id =1 系统异常</a> <br/>
<a href="${pageContext.request.contextPath}/demo04/item.action?id=2">异常处理 id =2 自定义异常 </a> <br/>
<a href="${pageContext.request.contextPath}/demo04/item.action?id=3">异常处理正常</a> <br/>
方式1:实现接口
- 编写HandlerExceptionResolver接口的实现类CustomExceptionResolver
- 实现方法resolverException()
- 并根据参数Exception e对异常进行处理:自定义异常 系统异常
public class CustomExceptionResolver implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
CustomExcption customExcption = null;
if(e instanceof CustomExcption) {
customExcption = (CustomExcption) e;
customExcption = new CustomExcption("系统繁忙,请稍后重试!");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message" ,customExcption.getMessage());
modelAndView.setViewName("forward:/error.jsp");
return modelAndView;
方式二:通知类
- 编写GlobalExceptionResolver类 用于处理所有异常
- 在类上添加@Controller的增强器@ControllerAdvice
- 在对应的方法上添加@ExceptionHandler来捕获异常
public class GlobalExceptionResolver {
@ExceptionHandler(CustomExcption.class)
public String custom(CustomExcption ec, Model model) {
model.addAttribute("message", ec.getMessage() + "Global");
return "forward:/error.jsp";
@ExceptionHandler(Exception.class)
public String other(Exception e, Model model) {
model.addAttribute("message", "系统繁忙,请稍后重试!" + "Global");
return "forward:/error.jsp";
异常信息展示页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
总结
视图解析器:
可以帮助我们快速修改视图路径 不用一个一个Controller去修改
方法返回值:
方法返回值默认是ModelAndView Model默认是request作用域 绕过视图解析器 使用请求转发 或者重定向
异常处理器:
更加友好的提示异常
本文使用 文章同步助手 同步