springbootWeb静态资源在这里,按readme的指示存放即可springbootWeb静态资源-Java文档类资源-CSDN下载,好像要钱,你们要和我私聊,我发给你们,源码也是。
建立一个空的springboot项目,如图建立一下文件夹
建立项目导入依赖时,导入springweb依赖,之后在导入一下lombok依赖就行
首先建立部门和员工的实体类
Department.java
//部门表
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
Employee.java
//员工表
@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(); //自定义有参构造函数,方便定义时间
}
}
编写对应的dao层,因为没有连接数据库,所以我们自己伪造一下数据
DepartmentDao
@Repository
public class DepartmentDao {
//因为没连接数据库,所以模拟数据库
private static Map<Integer, Department> departments =null;
static {
departments=new HashMap<Integer, Department>(); //创建一个部门表
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<Department> getDepartment(){
return departments.values();
}
public Department getDepartmentById(Integer id){
return departments.get(id);
}
}
EmployeeDao
@Repository
public class EmployeeDao {
private static Map<Integer, Employee> employees =null;
private DepartmentDao departmentDao;
static {
employees=new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "AA", "956808458@qq.com", 0, new Department(101, "教学部")));
employees.put(1002, new Employee(1002, "BB", "956808458@qq.com", 0, new Department(102, "市场部")));
employees.put(1003, new Employee(1003, "CC", "956808458@qq.com", 0, new Department(103, "教研部")));
employees.put(1004, new Employee(1004, "DD", "956808458@qq.com", 0, new Department(104, "运营部")));
employees.put(1005, new Employee(1005, "EE", "956808458@qq.com", 0, new Department(105, "后勤部")));
}
private static Integer initId=1006;
//增加
public 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<Employee> getAll(){
return employees.values();
}
//id查询员工
public Employee getEmployeeById(Integer id){
return employees.get(id);
}
//id删除员工
public void delete(Integer id){
employees.remove(id);
}
}
使用thymeleaf接管网页,首先要在html中导入约束
xmlns:th="http://www.thymeleaf.org
对应的本地src需要使用th:href="@{}",这种东西需要参考着文档看
实现国际化(网页语言)
将idea 的编码修改为UTF-8
在resources目录下新建文件夹 i18n (internationalization的缩写),建立对应目录
在zh_CN或者en_US中选中如图所示,可以简化代码对应的代码如图,在longin.properties中使用zh_CN的代码就行
在配置文件中使用 spring.messages.basename=i18n/login 修改我们要使用的配置,在html的对应标签上使用thymeleaf接管
为了实现通过选项更换语言,我们需要自己实现方法,在配置类中新建MyLocaleResolver类
通过参数传递选项
public class MyLocaleResolver implements LocaleResolver { //实现了这个接口之后,springboot会先识别是否有用户自定义的国际化标准,没有就用默认的
@Override
public Locale resolveLocale(HttpServletRequest request) {
//获取请求中的语言参数
String language = request.getParameter("l");
Locale locale = Locale.getDefault(); //没有就是用默认的
//如果请求携带了国际化参数 // zh_CN
if(!StringUtils.isEmpty(language)){
String[] split = language.split("_");
locale = new Locale(split[0],split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
}
}
参数应该通过html传递过来,所以在html的对应标签下添加一个href,如图
还需要在springmvc中注册
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
将需要的标签替换为th:接管。
登录页面实现,需要判断用户名和密码,在index中传递的参数,通过controller判断
在controller中接收参数,下图是MyMvcConfig新增的一句
LoginController
@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"; //由于在MVC配置过这个页面转发,所以直接重定向到main.html就可以了
}else {
model.addAttribute("msg","用户名或密码错误");
return "index";
}
}
}
对于html页面重复的代码,我们通过 thymeleaf 的 th:fragment定义,th:replace或者th:insert插入到需要的位置。通过观察可以看到页眉和左侧边框的代码是重复使用的,所以我们新建一个html来储存这两个代码块,目录如图
将重复的代码块移动到此处,在块中使用 th:fragment 定义,在使用时需要使用 th:replace或者th:insert取出。
对于选中之后不高亮,通过观察代码可以看出是使用active使选中模块高亮,通过三元运算符实现选中高亮操作(第三行)
class="nav-link"
class="nav-link active"、
<a th:class="${active=='main.html'?'nav-link active':'nav-link'}" th:href="@{/index.html}">
新增一个员工的controller,首先写获取所有员工的方法,将list.html中的table修改一下,换成我们数据对应的格式
@Controller
public class EmployeeController {
@Autowired
EmployeeDao employeeDao;
@RequestMapping("/emps")
public String list(Model model){
Collection<Employee> employees=employeeDao.getAll();
model.addAttribute("emps",employees);
return "emp/list";
}
}
<table class="table table-striped table-sm">
<thead>
<tr>
<th>id</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>department</th>
<th>birth</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="emp:${emps}">
<td th:text="${emp.getId()}"></td>
<td th:text="${emp.getLastName()}"></td>
<td th:text="${emp.getEmail()}"></td>
<td th:text="${emp.getGender()==0?'女':'男'}"></td>
<td th:text="${emp.getDepartment()}"></td>
<td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
<th>
<button class="btn btn-sm btn-primary">修改</button>
<button class="btn btn-sm btn-danger">删除</button>
</th>
<!--<a href="">修改</a>|<a href="" style="color: red">删除</a>-->
</tr>
</tbody>
</table>
修改时,在对应的html中需要给出对应的name,这样才能把前端的数据传递给后端。代码暂时不附加,最后加上。
删除更简单,一个controller就解决了。
源代码:q956808459,私聊我吧,我发给你们,链接好像要钱。