目录
WebMvcConfigurer
视图解析器
异常处理器
拦截器
WebMvcConfigurer
1.概述
WebMvcConfigurer配置类其实是Spring
内部的一种配置方式,采用JavaBean
的形式来代替传统的xml
配置文件形式进行针对框架个性化定制,可以自定义一些Handler,Interceptor,ViewResolver,MessageConverter。基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer
接口;
在Spring Boot 1.5版本都是靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器,消息转换器等。SpringBoot 2.0 后,该类被标记为@Deprecated(弃用)。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport,方式一实现WebMvcConfigurer接口(推荐),方式二继承WebMvcConfigurationSupport类
视图解析器
实现:
1.实现WebMvcConfigurer接口
2.重写configureViewResolvers 该方法是用来配置视图解析器的 该方法有一个参数ViewResolverRegistry是一个注册器 用来注册你想定义的视图解析器
代码语言:javascript复制 /**
* Configure view resolvers to translate String-based view names returned from
* controllers into concrete {@link org.springframework.web.servlet.View}
* implementations to perform rendering with.
* @since 4.1
*/
default void configureViewResolvers(ViewResolverRegistry registry) {
}
3.添加@EnableWebMvc 开启个性化定制
4.覆盖configurerViewResolvers
代码语言:javascript复制@Configuration
@ComponentScan(basePackages="com.czxy.mvc.controller")
@EnableWebMvc
public class MvcConfiguration implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/pages/",".jsp");
}
}
异常处理器
异常处理器的实现方式有两种
第一种:实现类 编写实现类实现HandlerExceptionResolver接口
第二种:增强类
使用@ControllerAdvice对Controller进行增强
使用ExceptionHandler用于捕获控制器里面的异常 并进行处理?
方式一:实现HandlerExceptionResolver接口
代码语言:javascript复制@Component
public class CustomExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
// 1 统一异常
CustomExcption customExcption = null;
if(e instanceof CustomExcption) {
customExcption = (CustomExcption) e;
} else {
customExcption = new CustomExcption("系统繁忙,请稍后重试!");
}
// 2 错误信息返回
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message" ,customExcption.getMessage());
modelAndView.setViewName("forward:/error.jsp");
return modelAndView;
}
}
方式二:通知类
@CotrollerAdvice 全局异常处理
@ExceptionHandler 用来统一处理方法异常
代码语言:javascript复制@ControllerAdvice
public class GlobalExceptionResolver {
/**
* 自定义异常处理器
* @param ec
* @param model
* @return
*/
@ExceptionHandler(CustomExcption.class)
public String custom(CustomExcption ec, Model model) {
model.addAttribute("message", ec.getMessage() "Global");
return "forward:/error.jsp";
}
/**
* 其他异常处理器
* @param e
* @param model
* @return
*/
@ExceptionHandler(Exception.class)
public String other(Exception e, Model model) {
model.addAttribute("message", "系统繁忙,请稍后重试!" "Global");
return "forward:/error.jsp";
}
}
拦截器
preHandler:拦截器之前执行 返回true继续执行 返回false结束
postHandler:执行完Controller之后执行
afterCompletion:视图渲染完成之后完成
拦截器:
代码语言:javascript复制@Component
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("1 拦截前");
//放行
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("3 执行中");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("5 最后完成");
}
}
实现WebMvcConfigurer接口 重写addInterceptor方法 设置拦截路径
代码语言:javascript复制@Configuration //配置类
@ComponentScan(basePackages = {"com.czxy.inter.controller","com.czxy.inter.interceptor"})
@EnableWebMvc
public class SpringMVCConfig implements WebMvcConfigurer {
@Resource
private MyInterceptor myInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
InterceptorRegistration interceptorRegistration1 = registry.addInterceptor(myInterceptor);
interceptorRegistration1.addPathPatterns("/**");
}
}