模板引擎页面刷新实时生效
-
禁用缓存
spring.thymeleaf.cache=false -
IDEA Ctrl+ F9 重新编译
- LoginController
@Controller
public class LoginController {
@PostMapping(value = "/user/login")
// @RequestMapping(value = "/user/login", method = RequestMethod.POST)
public String Login(@RequestParam("username") String username,
@RequestParam("password") String password,
Map<String,Object> map,
HttpSession httpSession){
if(!StringUtils.isEmpty(username) && password.equals("123456"))
{
//防止重复提交表单,重定向到主页
httpSession.setAttribute("loginUser",username);
return "redirect:/main.html";
}else{
map.put("msg","用户名密码错误");
return "login";
}
}
}
- 拦截器
/*
*登陆检查
*/
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 {
}
}
- 在config中注册拦截器
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
// super.addViewControllers(registry);
registry.addViewController("/panwang").setViewName("success");
}
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index.html","/","/user/login");
}
@Bean
public WebMvcConfigurerAdapter webMvcAutoConfigurationAdapter(){
WebMvcConfigurerAdapter adpter = new WebMvcConfigurerAdapter(){
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
};
return adpter;
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
模板文件