13. Spring Boot 拦截器

1.thymeleaf  页面修改后可能不会实时反馈到Web,做2步骤:

  1)禁用掉tymleaf 缓存:  spring.thymeleaf.cache=false

  2)IDE编辑器:Ctrl + F9  重新编译页面源码文件

2.if不为空判断:

string也又isEmpty()方法  详见文档

防止表单重复提交:重定向

自定义拦截器:

 1 /**
 2  * 登陆检查,
 3  */
 4 public class LoginHandlerInterceptor implements HandlerInterceptor {
 5     //目标方法执行之前
 6     @Override
 7     public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
 8         Object user = request.getSession().getAttribute("loginUser");
 9         if(user == null){
10             //未登陆,返回登陆页面
11             request.setAttribute("msg","没有权限请先登陆");
12             request.getRequestDispatcher("/index.html").forward(request,response);
13             return false;
14         }else{
15             //已登陆,放行请求
16             return true;
17         }
18 
19     }
20 
21     @Override
22     public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
23 
24     }
25 
26     @Override
27     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
28 
29     }
30 }

2.配置拦截器;

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    
    /**
     * 浏览器发送addViewTest请求,来到success页面
     * 发请求到页面,就没有必要在Controller里写空方法了,直接来做视图映射
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       // super.addViewControllers(registry);
        //浏览器发送 /atguigu 请求来到 success
        registry.addViewController("/atguigu").setViewName("success");
    }
    
    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer adapter = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
                registry.addViewController("/cancleInvoice.html").setViewName("cancleInvoice");
                registry.addViewController("/addInvoice.html").setViewName("add");
            }

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //super.addInterceptors(registry);
                //静态资源;  *.css , *.js,springMvc需要配置
                //SpringBoot已经做好了静态资源映射,但是经检验,SpringBoot2.0.5得配置静态资源,可能是1.5版本不需要吧
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/index.html","/","/user/login","/static/**","/webjars/**","/asserts/**");
            }
        };
        return adapter;
    }

    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

}

在html页面中判断是否有值,有的话则显示session中的友情提示

<span id="cardOpenStatus"  th:text="${session.cardInfo.message}" th:if="${not #strings.isEmpty(session.cardInfo.message)}"></span>
原文地址:https://www.cnblogs.com/guchunchao/p/9992952.html