把准备的后台模板准备好,地址:
链接:https://pan.baidu.com/s/13mNCQ18_nl6DHpxfKl4ZFw
提取码:love
导所需要的依赖
org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
- 然后把网页模板都导入到templates文件夹下
2.把静态资源导入到static文件夹下
3.模拟数据库操作
pojo层创建
@Data
@AllArgsConstructor
@NoArgsConstructor
//部门表
public class Development {
private Integer id;
private String developmentName;
}
@Data
@NoArgsConstructor
//员工表
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;
private Development development;
private Date birth;
public Employee(Integer id, String lastName, String email, Integer gender, Development development) {
this.id = id;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.development = development;
this.birth=new Date();
}
}
dao层创建
@Repository
public class DevelopmentDao {
//模拟数据库管理数据
public static Map developments=null;
static {
developments=new HashMap();
developments.put(101,(new Development(101,"教育部")));
developments.put(102,(new Development(102,"人事部")));
developments.put(103,(new Development(103,"运营部")));
developments.put(104,(new Development(104,"技术部")));
developments.put(105,(new Development(105,"后勤部")));
}
//获取部门表的所有信息
public Collection getDevelopmentAll(){
return developments.values();
}
//通过获取id获得部门的信息
public Development getDevelopmentById(Integer id){
return developments.get(id);
}
}
@Repository
public class EmployeeDao {
//模拟数据管理员工表
public static Map employees=null;
static {
employees=new HashMap();
employees.put(1001,new Employee(1001,"Aa","A1157627585@qq.com",0,new Development(101,"教育部")));
employees.put(1002,new Employee(1002,"Bb","B1157627585@qq.com",1,new Development(102,"人事部")));
employees.put(1003,new Employee(1003,"Cc","C1157627585@qq.com",0,new Development(103,"运营部")));
employees.put(1004,new Employee(1004,"Dd","D1157627585@qq.com",1,new Development(104,"技术部")));
employees.put(1005,new Employee(1005,"Ee","E1157627585@qq.com",0,new Development(105,"后勤部")));
}
//获得所有员工的信息
public Collection getEmployeeAll(){
return employees.values();
}
//根据id获取员工的信息
public Employee getEmployeeById(Integer id){
return employees.get(id);
}
//主键自增
public static Integer initEmployeeid=1006;
//增加一个员工
public void addEmployee(Employee employee){
//如果添加的员工id为空
if (employee.getId()==null){
//那么就自动+1
employee.setId(initEmployeeid++);
}
//把所添加的信息添加到数据库中
employees.put(employee.getId(),employee);
}
//根据id删除一个员工
public void deleteEmployee(Integer id){
employees.remove(id);
}
}
扩展首页的mvc配置
//添加一个视图控制器,来控制跳转的方式
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index.html").setViewName("login");
registry.addViewController("/main.html").setViewName("index");
}
需要关闭thymleaf引擎的缓存机制
#关闭thymleaf缓存机制 spring.thymeleaf.cache=false
网页表头需要添加thymleaf的命名空间
xmlns:th="http://www.thymeleaf.org"
需要把网页改成thymleaf格式
所有页面的静态资源都需要使用thymleaf接管,
其他也都是需要改,在线的连接不需要改
首先需要修改File Encodings
创建i18n文件夹,并且创建login.properties
把网页修改成国际化
th:text:#{}来配置国际化信息
自定义一个组件LocaleResolver来控制语言的国际化
//解析请求
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取语言的请求
String language = request.getParameter("l");
Locale locale = Locale.getDefault();//如果没有所选的语言就是默认的
//如果获取的链接携带了国际化的参数
//如果选择的语言不为空
if(!StringUtils.isEmpty(language)){
//zh_CN
String[] split = language.split("_");
//国家,地区
locale = new Locale(split[0], split[1]);
}
return locale;
}
然后将自定义组件配置到spring容器中,也就是@Bean
//这个是为了实现国际化
public LocaleResolver localResolver(){
return new MyLocalResolver();
}
登录功能实现
因为数据库是伪造的,所以登录的时候无论什么都能登录进去
写一个登录的控制器LoginController
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(@RequestParam("username")String username,
@RequestParam("password")String pwd,
Model model, HttpSession session) {
System.out.println("debug==>"+username);
if (!StringUtils.isEmpty(username)&&"123456".equals(pwd)) {
session.setAttribute("loginUser", username);
return "redirect:/main.html";
} else {
model.addAttribute("msg", "密码或者用户名输入错误,请重新登录!");
return "login";
}
}
}
由于没有提示,所以需要在前端加一个标签来提示
展示登录页面
登录拦截器
创建一个拦截器方法LoginHandlerInterceptor,为了拦截那些没有登录就进入主界面的操作
public class LoginHandlerInterceptor 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;
}else {
return true;
}
}
}
把LoginHandlerInterceptor配置到spring容器中,@Bean.
//添加一个拦截器,为了拦截那些没有登录就进入主界面的操作
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("-->



