1. 首先是关于静态资源的问题
默认是将css,js放在static中,HTML放在templates下的,在写拦截器的时候记得将静态资源放开,如果不保险可以全部写上。
关于拦截器的书写,实现HandlerInterceptor接口,其实的使用pre方法
// 这里是拦截器,拦截登陆的
public class LoginHanderIntercepter implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 登陆成功后,应该有用户的session
Object loginUser = request.getSession().getAttribute("loginUser");
if(loginUser == null){
request.setAttribute("msg","没有权限,别乱试");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}
return true;
}
}
再将其注册到webMvcConfigurer中。
重写addInterceptors方法
@Configuration // 记得要写
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("zhuye");
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHanderIntercepter()).addPathPatterns("/**")
.excludePathPatterns("/index.html", "/","/user/login","/static/**","/static/**","/css/**","/img/**","/js/**");
}
}
2. 关于a链接的问题
链接链的是地址栏的地址,不是存储的位置,需要通过controller层来调用返回所在的页面,
<a th:class="${active=='main.html'?'nav-link active':'nav-link'}" th:href="@{/index.html}">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">
<path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
<polyline points="9 22 9 12 15 12 15 22"></polyline>
</svg>
首页 <span class="sr-only">(current)</span>
</a>
@Controller
public class MySelf1Controller {
@RequestMapping("/emp")
public String emp(){
return "manage";
}
}
3. 关于图片无法加载的问题
如果是放在static中,那么可以尝试直接在游览器输入图片的地址查看,如果无法显示的话,有可能是图片没有打包进入项目中,
点击maven中的clean,再点击package。