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

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

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

2.if不为空判断:

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

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

自定义拦截器:

 /**
* 登陆检查,
*/
public class LoginHandlerInterceptor implements HandlerInterceptor {
//目标方法执行之前
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object user = request.getSession().getAttribute("loginUser");
if(user == null){
//未登陆,返回登陆页面
request.setAttribute("msg","没有权限请先登陆");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}else{
//已登陆,放行请求
return true;
} } @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}

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>

13. Spring Boot 拦截器的更多相关文章

  1. Spring boot拦截器的实现

    Spring boot拦截器的实现 Spring boot自带HandlerInterceptor,可通过继承它来实现拦截功能,其的功能跟过滤器类似,但是提供更精细的的控制能力. 1.注册拦截器 @C ...

  2. 【spring boot】spring boot 拦截器

    今日份代码: 1.定义拦截器 import com.alibaba.fastjson.JSON; import org.apache.commons.collections.CollectionUti ...

  3. spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...

  4. spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案(转)

    文章转自 http://blog.51cto.com/12066352/2093750 最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2 ...

  5. spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案

    Springboot中静态资源和拦截器处理(踩了坑)   背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到spr ...

  6. spring boot拦截器

    实现自定义拦截器只需要3步: 1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口. 2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addI ...

  7. (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】

    上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系.     Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...

  8. Spring boot 拦截器和过滤器

    1. 过滤器 Filter介绍 Filter可以认为是Servlet的一种“加强版”,是对Servlet的扩展(既可以对请求进行预处理,又可以对处理结果进行后续处理.使用Filter完整的一般流程是: ...

  9. spring boot 拦截器

    @SpringBootApplicationpublic class Application extends WebMvcConfigurerAdapter { public static void ...

随机推荐

  1. K3CLOUD安装教程

    1.安装SQLSERVER2008 2.安装K3CLOUD安装包,此处各种安装iis,tomcat,ftp等环境,有过it经验的应该都能自己搞定,不详细赘述 3.进入管理中心,进行设置,默认为127. ...

  2. hadoop集群故障排除

    故障一:某个datanode节点无法启动 我是以用户名centos安装和搭建了一个测试用的hadoop集群环境,也配置好了有关的权限,所有者.所属组都配成centos:centos [故障现象] 名称 ...

  3. Jenkins之前置替换脚本内容

    在执行Jenkins任务前,需要修改执行的工程的某个文件中的内容,在前置步骤中编写脚本进行修改. Pre Steps Windows batch script @echo off CHCP setlo ...

  4. 聊聊我怎么系统学习Linux技能并快速提高的

    随着电子信息科技时代的发展,学会使用计算机在我们的生活中成为了必不可少的一项技能.而作为计算机中的三大操作系统之一的Linux更是饱受计算机爱好者们的喜爱.今天我们就来和大家一起聊一聊Linux操作系 ...

  5. jenkins--使用命令行自动启动Jenkins的job

    Jenkins作为持续集成强大的开源工具,除了使用界面它还有强大的cli命令. 1 自动启动jenkins 的job: 启动不带参数的job: curl --user USER:PASSWORD JE ...

  6. mvc 验证登录

    很多时候,我们需要多个页面验证用户是否登录 有2中方法. 一种是继承 Attrbuite属性,添加验证,这个可以网上搜索. 我一般使用下面的方式 创建BaseWebController继承Contro ...

  7. python 操作系统模块 -- OS

    os,语义为操作系统,模块提供了访问多个操作系统服务的功能,可以处理文件和目录这些我们日常手动需要做的操作.os和它的子模块os.path还包括一些用于检查.构造.删除目录和文件的函数,以及一些处理路 ...

  8. Spring的 AOP底层用到两种代理机制

    JDK 的动态代理:针对实现了接口的类产生代理.CGlib 的动态代理:针对没有实现接口的类产生代理,应用的是底层的字节码增强的技术 生成当前类的子类对象 JDK动态代理实现1. 创建接口和对应实现类 ...

  9. UVa - 12050

    A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,the ...

  10. mysql case when 判断null

    select  name,case WHEN m.NAME is null THEN '' else m.NAME end NAME1 from  sys_users