springmvc扩展器
@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("dashboard");
}
区域解析器
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String l = request.getParameter("l");
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(l)){
String[] s = l.split("_");
locale = new Locale(s[0],s[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
登录拦截器
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object loginUser = request.getSession().getAttribute("loginUser");
if(loginUser == null){
request.setAttribute("msg","请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
return false;
}
return true;
}
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").
excludePathPatterns("/index.html","/","/user/login","/asserts/**");
}
thymeleaf相关
公共部分
- 创建common包下commons.html
- 将公共部分在页面下编写,并 th:fragment=“id”
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topBar">
...
</nav>
- 在要插入的部分
<div th:insert="~{common/commons::topBar}"></div>
<div th:replace="~{common/commons::sideBar(active='main.html')}"></div>
- 若有区别,可在括号里传参,如上
<a th:class="${active=='main.html'?'nav-link active':'nav-link'}" th:href="@{/main.html}">
比较
<td th:text="${emp.getSex()}==0?'女':'男'"></td>
一些数据类型
<td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
<p style="color: darkred" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
IDEA热部署
参考
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>