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. [转帖]速度快散热好 为什么U.2 SSD还没普及?

    速度快散热好 为什么U.2 SSD还没普及?   经典的影视剧中总有那么几位武林高手,江湖上只闻其名,不见其形.今天我们要聊的这位爷,誓要拳打南山M.2,脚踩北海SATA 3!它就是固态硬盘界久负盛名 ...

  2. Laravel 常见错误 1071 Specified key was too long

    Laravel 5.5 + Mysql 5.5 ,执行 migrate 时,提示索引长度超过指定的 1000 bytes 原因: Mysql 对索引有一定的长度限制,版本不同长度不同: MyIsAm ...

  3. 在 Linux 虚拟机中手动安装或升级 VMware Tools

    对于 Linux 虚拟机,您可以使用命令行工具手动安装或升级 VMware Tools. 本次Linux 虚拟机为CentOS6.5 先决条件开启虚拟机.确认客户机操作系统正在运行.由于 VMware ...

  4. 我是如何沉迷于linux系统的?

    Linux?这个对大多数人来说,是一个陌生的词.曾经的我,对这个也是一无所知的,我没有编程背景,我的专业知识是英语,而不是计算机语言. 我是如何和这个词搭上联系的呢?还是缘于一段一次奇妙的社团活动,我 ...

  5. EF 跨库查询

    原因:最近公司项目,遇到一个ef跨库查询的问题.(只是跨库,并不是跨服务器哈) 主要我们的一些数据,譬如地址,城市需要查询公共资料库. 但是本身我的程序设计采用的是ef框架的.因此为这事花费了1天时间 ...

  6. mvc 验证登录

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

  7. 聪聪和可可 HYSBZ - 1415(概率 + spfa + 记忆化dp)

    Input 数据的第1行为两个整数N和E,以空格分隔,分别表示森林中的景点数和连接相邻景点的路的条数. 第2行包含两个整数C和M,以空格分隔,分别表示初始时聪聪和可可所在的景点的编号. 接下来E行,每 ...

  8. 【刷题】UOJ #374 【ZJOI2018】历史

    九条可怜是一个热爱阅读的女孩子. 这段时间,她看了一本非常有趣的小说,这本小说的架空世界引起了她的兴趣. 这个世界有 \(n\) 个城市,这 \(n\) 个城市被恰好 \(n-1\) 条双向道路联通, ...

  9. ORACLE常用性能监控SQL

    Temp表空间上进程的查询 select a.tablespace, b.sid, b.serial#, a.blocks,c.sql_text from v$sort_usage a,v$sessi ...

  10. php框架:Flight 简介

    Flight是一个php的极简的有着微内核的框架,能过快速的构建RESTful的应用 官网地址: http://flightphp.com/ github地址:https://github.com/m ...