一、准备工作
1.静态资源及导入
.html放入templates中,asserts中的文件放入static中

2.写pojo和dao

二、首页实现
MyMvcConfig.java
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
首页配置-注意点:
所有页面的静态资源都需要使用thymeleaf接管
1.在html文件前导入命名空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
2.使用thymeleaf语法
url: th:href="@{/目录}"
三、页面国际化(适配中英文)
1.配置i18n文件
(1)在resources下建立分组 i18n (internationalization);
(2)在i18n下创建配置文件login.propertie(默认)和login_zh_CN.properties(中文);
(3)上面两个文件建好后,会自动创建并被分到Resource Bundle 'login’分组;

(4)自己手动添加英文配置文件(en_US英文)
2.Resource Bundle


可视化配置中,可设置三个配置文件
3.修改静态资源html文件
4.注意点:
(1)如果我们需要在项目中进行按钮自动切换中英文,需要自定义一个组件LocaleResolver
(2)记得将自己写的组件配置到spring容器中 Bean
(3)#{}
四、登录功能实现
新建controller下的类LoginControlle
package com.kuang.springboot03web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
Model model){
//具体的业务:
if(!StringUtils.isEmpty(username) && "123456".equals(password)){
return "redirect:/main.html";
}else {
//告诉用户,你登录失败了!
model.addAttribute("msg","用户名或者密码错误!");
return "index";
}
}
五、登录拦截器
新建config下的类LoginHandlerIntercepto,继承于HandlerInterceptor
重写public boolean preHandle方法
package com.kuang.springboot03web.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
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 {
//登录成功之后,应该有用户的session;
Object loginUser = request.getSession().getAttribute("loginUser");
if(loginUser==null){//没有登录
request.setAttribute("msg","没有权限,请先登录");
request.getRequestDispatcher("/index.html").forward(request,response);
}else {
return true;
}
return false;
}
}
六、展示员工列表
七、增加员工实现
八、修改员工信息
九、删除及404处理
版权声明:本文为weixin_45686473原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。