去除模板引擎的缓存





th:if  优先级高于  th:text



登陆成功,防止表单重复提交,可以重定向到主页



只有登录之后才能访问相关的页面

login.html

<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="description" content=""><meta name="author" content=""><title>登录</title><!-- Bootstrap core CSS --><link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet"><!-- Custom styles for this template --><link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet"></head><body class="text-center"><form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post"><img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" src="asserts/img/bootstrap-solid.svg" alt="" width="72" height="72"><h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1><!--判断--><p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p><label class="sr-only" th:text="#{login.username}">Username</label><input type="text"  name="username" class="form-control" placeholder="Username" th:placeholder="#{login.username}" required="" autofocus=""><label class="sr-only" th:text="#{login.password}">Password</label><input type="password" name="password" class="form-control" placeholder="Password" th:placeholder="#{login.password}" required=""><div class="checkbox mb-3"><label><input type="checkbox" value="remember-me"/> [[#{login.remember}]]</label></div><button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button><p class="mt-5 mb-3 text-muted">© 2019-2020</p><a class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a><a class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a></form></body></html>

LoginController.java

package com.atguigu.springboot.controller;import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpSession;
import java.util.Map;@Controller
public class LoginController {//@RequestMapping(value = "/user/login",method = RequestMethod.POST)@PostMapping(value = "/user/login")public String login(@RequestParam("username") String username,@RequestParam("password") String password,Map<String,Object> map, HttpSession session){if(!StringUtils.isEmpty(username) && "123456".equals(password)){//登陆检查,session.setAttribute("loginUser",username);//登陆成功,防止表单重复提交,可以重定向到主页return "redirect:/main.html";}else{//登陆失败map.put("msg","用户名密码错误");return  "login";}}
}

LoginHandlerInterceptor.java      登陆检查

package com.atguigu.springboot.component;import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;/*** 登陆检查*/
public class LoginHandlerInterceptor implements HandlerInterceptor {//目标方法执行之前@Overridepublic 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;}}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}
}

MyMvcConfig.java

package com.atguigu.springboot.config;import com.atguigu.springboot.component.LoginHandlerInterceptor;
import com.atguigu.springboot.component.MyLocaleResolver;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// super.addViewControllers(registry);//浏览器发送 /atguigu 请求来到 successregistry.addViewController("/atguigu").setViewName("success");}//所有的WebMvcConfigurerAdapter组件都会一起起作用@Bean //将组件注册在容器public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("login");registry.addViewController("/index.html").setViewName("login");registry.addViewController("/main.html").setViewName("dashboard");}//注册拦截器@Overridepublic void addInterceptors(InterceptorRegistry registry) {//super.addInterceptors(registry);//静态资源;  *.css , *.js//SpringBoot已经做好了静态资源映射registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login");}};return adapter;}@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}}



直接访问 http://localhost:8080/crud/main.html  会出现访问不了。要先登录才能访问

开发期间模板引擎页面修改以后,要实时生效 || 登陆成功,防止表单重复提交,可以重定向||只有登录之后才能访问相关的页面相关推荐

  1. php 返回页面重复提交,php防止表单重复提交

    后端防止重复提交的基本原理: 服务器返回表单页面时,会先生成一个subToken保存于session,并把该subToen传给表单页面.当表单提交时会带上subToken,服务器获取表单信息判断ses ...

  2. 【BUG修复】修改表单,提交后,提示登录状态已过期

    最近在设备管理中,新增了一点功能. 然后我悲哀的发现,特种设备和安全附件的修改功能不能用了. 每次修改页面,点击提交后,都会弹出这个页面: 但是诡异的是,新增反而没问题. 我找了各种方式,最后甚至找到 ...

  3. 防止刷新页面造成表单重复提交

    public partial class _Default : System.Web.UI.Page {     protected void Page_Load(object sender, Eve ...

  4. HTML页面防止表单重复提交

    情况一:使用ajaxForm提交表单 <form name="frm" id="frm" method="post" action=& ...

  5. Ajax(jQuery封装),表单form提交(Ajax),art-template模板引擎,原生Ajax,XML和JSON,axios,跨域和JSONP,防抖和节流,HTTP协议

    目录 服务器基本概念 1.URL 2.URL地址的组成部分 3. 图解客户端与服务器的通信过程 4.网页中如何请求数据 5.资源的请求方式 Ajax jQuery中的Ajax 1. $.get()函数 ...

  6. Mendix敏捷开发零基础学习《二》-进阶(Microflow微流、表单验证、运算符、条件判断、数据嵌套、触发器、Debug问题跟踪、版本管理)

    目录结构 Mendix敏捷开发零基础学习<二> 一.Microflow微流 1.引言 2.常见的功能 3.微流可以做那些事情? 3.1 举例1(用微流打开新增页面) 3.2 举例2(用微流 ...

  7. django一个html先后两个form,django 一个页面两个表单 怎么提交

    收到一只叮咚 1234567891011121314151617181920212223242526272829办法1.同一个页面中建立两个表单 各自提交: 1234567812345678办法2:如 ...

  8. 用js将form表单同时提交到两个不同页面的方法

    用js将form表单同时提交到两个不同页面的方法: <script type="text/javascript"> function dosubmit(){ windo ...

  9. php中在使用js_提交的表单不为空_为什么显示等于,php编程,这段代码为什么不能阻止表单的提交!不管为不为空 都跳转到1.php页面啦 这是怎么回事?...

    php编程,这段代码为什么不能阻止表单的提交!不管为不为空 都跳转到1.php页面啦 这是怎么回事? 关注:292  答案:5  mip版 解决时间 2021-01-12 06:43 提问者夜落花台 ...

最新文章

  1. Date, TimeZone, MongoDB, java中date的时区问题
  2. 【Python】 文件目录比较工具filecmp和difflib
  3. 通过MATLAB将数据转化为mif文件,供Quartusii软件的ROM核读取调用
  4. Android属性动画 Interpolator
  5. [2020.11.25NOIP模拟赛]下棋【dp】
  6. zip和unzip上的Java要点
  7. [剑指offer][JAVA]面试题第[15]题[二进制中1的个数][位运算]
  8. Pandas 文本数据方法 capitalize( ) lower( ) upper( )
  9. 程序人生001--点滴感悟-随笔01
  10. “华为搜索”正海外内测;苹果5亿美元和解“降速门”;Firefox隐藏HTTPS | 极客头条...
  11. 【数据结构笔记40】哈希表冲突处理方法:开放地址法(线性探测、平方探测、双散列、再散列),分离链接法
  12. DFT compiler极简示例2(使用autofix)
  13. 苹果计算机怎么隐藏应用,苹果电脑怎么隐藏界面图标
  14. 电容器指南(一)-基本性能介绍
  15. 马原复习知识点背诵-《马克思主义基本原理概论》
  16. js用blob下载pdf文件打开后中文乱码
  17. Python-基础课-第二节-02-变量与常量
  18. 十年老程序员开始新事业
  19. 支持5G WIFI的串口服务器
  20. Windows 打开 Docker Desktop 出现错误:Docker failed to initialize

热门文章

  1. python设计模式-观察者
  2. HTML5新增表单验证
  3. JS实现图片的不间断连续滚动
  4. WPF ,listbox,平滑滚动的2种方式。
  5. C++指针初始化总结
  6. iBatis 的插入一个实体
  7. C#后台线程和UI的交互
  8. 如何设置Winform控件的ClientRectangle
  9. Wi-Fi速度慢的十个原因以及解决办法
  10. oracle导出脚本文件怎么打开,Windows下的Oracle导出脚本 -电脑资料