开发期间模板引擎页面修改以后,要实时生效 || 登陆成功,防止表单重复提交,可以重定向||只有登录之后才能访问相关的页面
生活随笔
收集整理的這篇文章主要介紹了
开发期间模板引擎页面修改以后,要实时生效 || 登陆成功,防止表单重复提交,可以重定向||只有登录之后才能访问相关的页面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
去除模板引擎的緩存
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? 會出現訪問不了。要先登錄才能訪問?
總結
以上是生活随笔為你收集整理的开发期间模板引擎页面修改以后,要实时生效 || 登陆成功,防止表单重复提交,可以重定向||只有登录之后才能访问相关的页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 默认访问首页 || 国际化||设置全局字
- 下一篇: CRUD-员工列表 大体流程