Springboot使用Bootstrap和thymeleaf路径问题
这是项目结构
引入bootstrap依赖之后,目录结构
- application.properties重点在这行代码
spring.mvc.static-path-pattern=/static/**
设置静态资源映射路径
spring.messages.basename=i18n.login
server.port=8888
spring.thymeleaf.cache=false
spring.mvc.static-path-pattern=/static/**
login.html
重点在:
href
路径填写自己项目中静态资源的访问路径,和之前在application.properties
文件中写的静态资源映射一致,如下
th:href
的路径根据依赖中的目录结构编写
<link href="/static/css/bootstrap.min.css"
th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.min.css}" rel="stylesheet" />
<link href="/static/css/signin.css"
th:href="@{/static/css/signin.css}" rel="stylesheet" />
/**
如果没有配置静态资源路径,默认取/**静态资源的文件夹里面查找,
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径
**/
<link th:href="@{/css/signin.css}" rel="stylesheet">
//这种也是可以的鼠标点不进去,但是能访问http://localhost:8888/css/signin.css
如果路径编写成功按ctrl键的同时点击路径会有下划线
并跳转导该文件,如果没有则是路径写错了
完整html文件
<!DOCTYPE html>
<!-- saved from url=(0051)https://getbootstrap.com/docs/4.1/examples/sign-in/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!--<html lang="en">-->
<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="" />
<link rel="icon" href="https://getbootstrap.com/favicon.ico" />
<title>登录页面</title>
<!-- Bootstrap core CSS -->
<link href="/static/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.min.css}" rel="stylesheet" />
<!-- Custom styles for this template -->
<link href="/static/css/signin.css" th:href="@{/static/css/signin.css}" rel="stylesheet" />
</head>
<body class="text-center" th:inline="text">
1
<form class="form-signin" th:action="@{/user/login}" method="post">
<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 for="inputPassword" class="sr-only" th:text="#{login.password}">Password</label>
<input type="password" name="password" id="inputPassword" 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">© 2017-2018</p>
<a href="#" class="btn btn-sm" th:href="@{/index.html(l='zh_CN')}">中文</a>
<a href="#" class="btn btn-sm" th:href="@{/index.html(l='en_US')}">English</a>
</form>
</body>
</html>
自定义拦截器:
package com.lx.spbtcrud.component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @ClassName LoginHandlerInteceptor
* @Description: 拦截器,拦截未登录用户对其他页面的访问
* @Author LX
* @Date 2019/11/19
* @Version V1.0
**/
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) {
return true;
}else{
request.setAttribute("msg","请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}
}
@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 {
}
}
注册拦截器:
- /项目根目录下
- /*拦截一层,例如:拦截
addPathPatterns("/a/*")
,则路径为/a/b
会被拦截,/a/b/c
不会拦截 - /**拦截两层,即
/a/b/c
也会被拦截。
注意:
- springboot2.0+版本不会自动映射静态资源,需要自己添加排除。
- 此前html文件中
href
用到的静态文件是在static目录下的,th:href
所用的的文件是webjars目录下, - 加上
excludePathPatterns("/index.html","/","/user/login","/static/**","/webjars/**");
解决问题
package com.lx.spbtcrud.config;
import com.lx.spbtcrud.component.LoginHandlerInterceptor;
import com.lx.spbtcrud.component.MyLocaleResolver;
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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
public void addViewController(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("success");
}
@Bean //註冊到容器去
public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
}
//注册拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").
excludePathPatterns("/index.html","/","/user/login","/static/**","/webjars/**");
}
};
return adapter;
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}