Department.java
package com.hiss.springboot03web.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
Employee.java
package com.hiss.springboot03web.pojo;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
//员工表
@Data
@NoArgsConstructor
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender; //0女1男
private Department department;
private Date birth;
public Employee(Integer id, String lastName, String email, Integer gender, Department department) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.department = department;
this.birth = new Date(); //自动生成默认日期
}
}
3.模拟数据库中的数据
DepartmentDao.java
package com.hiss.springboot03web.dao;
import com.hiss.springboot03web.pojo.Department;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
//部门dao
@Repository
public class DepartmentDao {
//模拟数据库中的数据
private static Map departments = null;
static {
//创建一个部门表
departments = new HashMap();
departments.put(101,new Department(101,"教学部"));
departments.put(102,new Department(102,"市场部"));
departments.put(103,new Department(103,"教研部"));
departments.put(104,new Department(104,"运营部"));
departments.put(105,new Department(105,"后勤部"));
}
//获得所有部门信息
public Collection getDepartments(){
return departments.values();
}
//通过id得到部门
public Department getDepartmentById(Integer id){
return departments.get(id);
}
}
EmployeeDao.java
package com.hiss.springboot03web.dao;
import com.hiss.springboot03web.pojo.Department;
import com.hiss.springboot03web.pojo.Employee;
import org.omg.CORBA.PRIVATE_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
//员工Dao
@Repository
public class EmployeeDao {
//模拟数据库中的数据
private static Map employees = null;
//员工有所属的部门
@Autowired
private DepartmentDao departmentDao;
static {
//创建一个部门表
employees = new HashMap();
employees.put(1001,new Employee(1001,"AA","A2265821702@qq.com",0,new Department(101,"教学部")));
employees.put(1002,new Employee(1002,"BB","B2265821702@qq.com",1,new Department(101,"市场部")));
employees.put(1003,new Employee(1003,"CC","C2265821702@qq.com",0,new Department(101,"教研部")));
employees.put(1004,new Employee(1004,"DD","D2265821702@qq.com",1,new Department(101,"运营部")));
employees.put(1005,new Employee(1005,"EE","E2265821702@qq.com",0,new Department(101,"后勤部")));
}
//主键自增
private static Integer initId = 1006;
//添加一个员工
private void save(Employee employee){
if (employee.getId()==null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
employees.put(employee.getId(),employee);
}
//查询员工全部信息
public Collection getAll(){
return employees.values();
}
//通过Id查询员工
public Employee getEmployeeById(Integer id){
return employees.get(id);
}
//通过id删除员工
public void delete(Integer id){
employees.remove(id);
}
}
首页映射
自定义扩展springmvc:
MyMvcConfig.java
package com.hiss.springboot03web.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration //让这个类变成配置类
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
}
此时首页如下:
此时静态资源没有加载进来 我们要根据thymeleaf模板引擎要求把index.html更改成thymeleaf所支持的东西
thymeleaf语法见Springboot入门笔记6
关闭模板引擎缓存
首页如下:
404.html
dashboard.html
list.html
但是如404页面中的这种在线链接是无需更改的
首先保证编码是utf-8
新建i18n目录
对应index页面将需要国际化的元素配置好(Resource Bundle:可视化配置)
绑定配置文件
更改国际化的格式
首页如下:
根据按钮自动切换中文英文:
在Spring中有一个国际化的Locale(区域信息对象)里面有一个叫做LocaleResolver(获取区域信息对象)的解析器
想点击链接让国际化资源生效,就需要让自己的Locale生效。写一个自己的LocaleResolver,在链接上携带区域信息
MyLocaleResolver.java
package com.hiss.springboot03web.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) {
}
}
配置 使自定义的国际化组件生效:
MyMvcConfig.java
//自定义的国际化组件
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
效果如下:
参考资料:狂神说



