我在渲染 html 视图并尝试显示从 MySQL 收集的对象的简单列表时遇到问题。我相信这应该是一个简单的问题,但我无法弄清楚,我已经搜索了数百个类似的问题,但无法解决我的问题。使用 mvn spring-boot:run 在 Java 控制台或 cmd 上均未显示任何日志
我能够使用 DAO 从 MySQL 正确收集信息,因为我将其打印在控制台上并且显示正常..将其打印在我的控制器上,如下所示
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import com.valuaciones_automoviles.afip.dao.VAutomovilesDao;
@Controller
public class VAutomovilesController {
@Autowired
VAutomovilesDao vaDao;
private final Logger logger = LoggerFactory.getLogger(VAutomovilesController.class);
@GetMapping("/")
public ModelAndView index(){
ModelAndView mv = new ModelAndView("index");
mv.addObject("vaItems", vaDao.getVa());
System.out.println(vaDao + "vaDao");
logger.info(mv.toString());
return mv;
}
}
当点击 localhost:8080:
时会打印以下内容休眠:选择valuacione0_.in作为in1_0_,valuacione0_.a2012作为a2_0_,valuacione0_.a2013作为a3_0_,valuacione0_.a2014作为a4_0_,valuacione0_.a2015作为a5_0_,valuacione0_.a2016作为a6_ 0_,valuacione0_.a2017 作为 a7_0_,valuacione0_.a2018 作为a8_0_、valuacione0_.a2019 为 a9_0_、valuacione0_.a2020 为 a10_0_、valuacione0_.a2021 为 a11_0_、valuacione0_.descripcion 为 descrip12_0_、valuacione0_.marca 为 marca13_0_、valuacione0_.tipo 为提示o14_0_ 来自 valuaciones_automoviles valuacione0_ com.valuaciones_automoviles.aip.dao.VAutomovilesDao@17143b3bvaDao 2022-10-06 22:15:51.273 信息 3516 --- [nio-8080-exec-1] c.v.a.controller.VAutomovilesController : ModelAndView [view="index";模型 = {vaItems = [ValuacionesAutomoviles [id = 1,marca =福特,descripcion = 4 puertas,tipo =汽车,a2021 = 2121,a2020 = 44,a2019 = 2123,a2018 = 321,a2017 = 432,a2016 = 543,a2015 =6, a2014=654, a2013=564, a2012=45]]}]
看起来不错
我的index.html页面是
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link th:rel="stylesheet" th:href="@{/webjars/bootstrap/3.4.1/css/bootstrap.min.css} " />
</head>
<body>
<div class="container">
<h1 class="text-center">Autos</h1>
<hr />
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Description</th>
<th>Complete</th>
</tr>
</thead>
<tbody>
<tr th:each="item : ${vaItems}">
<td th:text="${item.marca}">marca</td>
<td th:text="${item.descripcion}">descripcion</td>
</tr>
</tbody>
</table>
</div>
<script th:src="@{/webjars/jquery/3.6.0/jquery.min.js}"></script>
<script th:src="@{/webjars/bootstrap/3.4.1/js/bootstrap.min.js}"></script>
</body>
</html>
我的 POM 是:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.valuaciones_automoviles</groupId>
<artifactId>afip</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>afip</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<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-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>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.webjars/jquery -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是我的项目结构:
我没有添加 DAO 和 Entity 以及 Hibernate Util 类,因为它们看起来工作正常,因为我已经在打印对象了。任何帮助将不胜感激,如果需要任何额外信息,请告诉我
这是我在点击 localhost:8080 时得到的唯一日志,没有出现其他内容
2022-10-07 17:01:53.222 INFO 9408 --- [ main] c.v.afip.AfipApplication : 在 DESKTOP-6LF9U81 上使用 Java 14.0.2 启动 AfipApplication,PID 为 9408 (C:\Users\JOTA\Desktop\Jota new项目 fip fip arge