【发布时间】:2020-09-19 08:04:15
【问题描述】:
我正在使用 spring boot 创建简单的 crud 系统。当我加载页面时,它正在正常工作。当我添加一个新链接时,它显示为 HTTP 状态 404 – 未找到 Spring Boot 我不明白为什么我所做的一切都是正确的,我在下面的代码中附上了屏幕截图。
员工控制器
@Autowired
private EmployeeService service;
@RequestMapping("/")
public String viewHomePage(Model model) {
List<Employee> listemployee = service.listAll();
model.addAttribute("listemployee", listemployee);
return "index";
}
@RequestMapping(value = "/new")
public String add(Model model) {
model.addAttribute("employee", new Employee());
return "new";
}
index.html
<table class="table">
<tbody>
<tr>
<td>
<a th:href="@{'/new'}">Add new</a>
</td>
</tr>
<tr th:each="employee : ${listemployee}">
<td th:text="${employee.id}">Employee ID</td>
<td th:text="${employee.firstName}">FirstName</td>
<td th:text="${employee.lastName}">LastName</td>
<td>
<a th:href="@{'/edit/' + ${employee.id}}">Edit</a>
<a th:href="@{'/delete/' + ${employee.id}}">Delete</a>
</td>
</tr>
</tbody>
</table>
文件夹结构
porm.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/lindaschool?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
server.error.whitelabel.enabled=false
server.port=7020
spring.datasource.username=root
spring.datasource.password=
#logging.level.root=WARN
spring.jpa.open-in-view=false
spring.thymeleaf.cache=false
【问题讨论】:
-
我在上面添加了 pom.xml。如何添加这个 spring-boot-starter-thymeleaf
-
我已经添加了请检查上面的 pom.xml
-
application.properties 我在上面添加,请检查如何添加模板
-
您的
Employee.java文件看起来如何?工作部分(我猜这是/端点)正在返回一个EmployeeService,您很可能在其中调用Thymeleaf 来显示index.html。如果 Employee 类不是服务,而是模型,这当然不会起作用。 编辑:文件夹结构截图只列出student文件,Employee文件在哪里?有什么要开始的吗?
标签: spring spring-boot