resources
i18n 国际化 internationalization
参考:https://blog.csdn.net/qq_30081043/article/details/108305652
<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>
自定义拦截器
package com.wei.config;
import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
/*
自定义拦截器
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest request) {
String language = request.getParameter("l");
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(language)){
String[] split = language.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
通过配置注解@bean将我们自定义的国际化组件放进容器.
package com.wei.config;
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.WebMvcConfigurer;
import java.util.Locale;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
//定义国际化组件
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
}
//拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login",
"/ccs/**","/js/**","/img/**");
}
}
static
放css/img/js文件的地方
templates
放网页的地方.
commons
提取公共的代码,如侧边栏和顶部的html
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topbar">
<a class="navbar-brand col-sm-3 col-md-2 mr-0" th:href="@{/index.html}">[[${session.loginUser}]]</a>
<input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
<ul class="navbar-nav px-3">
<li class="nav-item text-nowrap">
<a class="nav-link" th:href="@{/exit}">退出</a>
</li>
</ul>
</nav>
复用:
<div th:replace="~{/commons/commons::topbar}"></div>
传递参数: 在尾部加括号
<div th:replace="~{/commons/commons::sidebar(active='main.html')}"></div>
emp
各个模块.例如客户/订单/图书等…可以建立多个文件夹进行分类.
error
错误页面.设置何种错误,就使用哪种的错误数字命名.
application.yaml
# 关闭模板引擎的缓存
spring:
thymeleaf:
cache: false
# 国际化
messages:
basename: i18n/login
# 设置表单上传的日期格式
mvc:
format:
date: yyyy-MM-dd
# 设置项目的访问路径
server:
servlet:
context-path: /wei
src
config
自定义拦截器:LoginHandlerInterceptor
package com.wei.config;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(request.getSession().getAttribute("loginUser")==null){
request.setAttribute("msg", "没有权限,请先登录");
request.getRequestDispatcher("/index.html").forward(request, response);
return false;
}else{
return true;
}
}
}
配置自定义拦截器
package com.wei.config;
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.WebMvcConfigurer;
import java.util.Locale;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
//配置视图的控制,例:访问/main.html 即bashboard
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
//拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login",
"/ccs/**","/js/**","/img/**");
}
}
thymeleaf的一些知识
- 导入名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
- $ 变量
- @ 用于链接
- each 复用
<select class="form-control" name="department.id">
<option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}">1</option>
</select>
- if 判断的使用
th:if="${not #strings.isEmpty(msg)}"
- 单选与下拉框
<input class="form-check-input" type="radio" name="gender" value="1" th:checked="${emp.gender==1?'checked':false}">
<option th:selected="${emp.department.getId()==dept.getId()}"
th:each="dept:${departments}"
th:text="${dept.getDepartmentName()}"
th:value="${dept.getId()}">1</option>
- 传递参数 RESTful风格
<a class="btn btn-sm btn-danger" th:href="@{/delete/}+${emp.id}" >删除</a>
-
复用带有 th:fragment=“sidebar” 的html 片段
使用insert和replace都可以复用.
- 传递消息的2种方式
标签属性
th:placeholder="#{login.password}"
直接放置在标签外
[[#{login.remember}]]
内置函数
参考API 19单元(90页)
th:value="${#dates.format(emp.birth,'yyyy-MM-dd')}
表单form
<form th:action="@{/emp}" method="post">
<div class="form-group">
<label for="name">name</label>
<input type="text" class="form-control" id="name" placeholder="请输入名字" name="lastName">
</div>
<div class="form-group">
<label for="email">email</label>
<input type="text" class="form-control" id="email" placeholder="请输入邮箱" name="email">
</div>
<div class="form-group">
<label >gender</label><br/>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="1">
<label class="form-check-label">男</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" value="0">
<label class="form-check-label">女</label>
</div>
</div>
<div class="form-group">
<label >department</label>
<select class="form-control" name="department.id">
<option th:each="dept:${departments}" th:text="${dept.getDepartmentName()}" th:value="${dept.getId()}">1</option>
</select>
</div>
<div class="form-group">
<label for="birth">birth</label>
<input type="text" class="form-control" id="birth" placeholder="请输入生日" name="birth">
</div>
<button type="submit" class="btn btn-default">添加</button>
</form>
pow.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wei</groupId>
<artifactId>springboot-03-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-03-web</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--thymeleaf,我们都是基于3.x开发的-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>