在list.html页面中使用/templates/emp 请求跳转到add.html页面中如果在Controller中写的是两层路径则无法加载样式。
list.html
<div class="container-fluid">
<div class="row">
<!--引入侧边栏-->
<div th:replace="commons/bar::#sidebar(activeUri='emps')"></div>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<h2><a class="btn btn-sm btn-success" href="" th:href="@{/templates/emp}">员工添加</a></h2>
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th>#</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>department</th>
<th>birth</th>
<th>操作</th>
</tr>
</thead>
add.html
<!DOCTYPE html>
<!-- saved from url=(0052)http://getbootstrap.com/docs/4.0/examples/dashboard/ -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Dashboard Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="asserts/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="asserts/css/dashboard.css" rel="stylesheet">
<!-- <style type="text/css">-->
<!-- /* Chart.js */-->
<!-- -->
<!-- @-webkit-keyframes chartjs-render-animation {-->
<!-- from {-->
<!-- opacity: 0.99-->
EmployeeController.java
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
// 查询所有的用户信息
@GetMapping(value = "/emps")
public String employees(Model model){
Collection<Employee> employees = employeeDao.getAll();
model.addAttribute("emps",employees);
return "emp/list";
}
//
@GetMapping(value = "/templates/emp")
public String toAddPage(){
return "emp/add";
}
}
解决方法:
1.将list.html和Controller中的路径改为一层/emp
2.将add.html中的加载css路径改为
<link href="/asserts/css/bootstrap.min.css" rel="stylesheet">
或者
<link href="../asserts/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="/asserts/css/dashboard.css" rel="stylesheet">
或者
<link href="../asserts/css/dashboard.css" rel="stylesheet">