【问题标题】:Thymeleaf does not display data from MySQLThymeleaf 不显示来自 MySQL 的数据
【发布时间】:2018-05-08 07:48:49
【问题描述】:

我是Spring 框架和Thymeleaf 的初学者。我编写了一个代码,它将在网站上显示来自数据库的数据。我将我的代码与MySQL 数据库集成。不知何故,当我查看localhost:8080 时,网站上根本没有显示数据。

控制者:

@Controller
public class HomeController {

private TicketService ticketService;

@Autowired
public void setTicketService(TicketService ticketService){
    this.ticketService = ticketService;
}

@RequestMapping(value = "/index",method = RequestMethod.GET)
public String list(Model model){
    model.addAttribute("ticket", ticketService.listAllTickets());

    return "index";

}
}

道:

 public interface TicketRepository extends CrudRepository<Ticket, Integer> {
 } 

模型

 public class Ticket {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private String ticket_station;
private String ticket_text;
private int ticket_created_at;
private String ticket_issuer;

public String getTicket_station() {
    return ticket_station;
}

public void setTicket_station(String ticket_station) {
    this.ticket_station = ticket_station;
}

public String getTicket_text() {
    return ticket_text;
}

public void setTicket_text(String ticket_text) {
    this.ticket_text = ticket_text;
}

public int getTicket_created_at() {
    return ticket_created_at;
}

public void setTicket_created_at(int ticket_created_at) {
    this.ticket_created_at = ticket_created_at;
}

public String getTicket_issuer() {
    return ticket_issuer;
}

public void setTicket_issuer(String ticket_issuer) {
    this.ticket_issuer = ticket_issuer;
}

}

服务:

public interface TicketService {
Iterable<Ticket> listAllTickets();

}

ServiceImpl:

 @Service
 public class TicketServiceImpl implements TicketService {
private TicketRepository ticketrepository;

@Autowired
public void setProductRepository(TicketRepository ticketRepository) {
    this.ticketrepository = ticketRepository;
}

@Override
public Iterable<Ticket> listAllTickets() {
    return ticketrepository.findAll();
}
}

index.html:

<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:th="http://www.thymeleaf.org">
 <head>
    <title>Ticket</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>

<style>
 table, th, td {
 border: 1px solid black;
 padding: 1px;
  }
  </style>
  </head>
  <body>
            <div>
                <table>

                    <tr>
                        <th>Ticket Station</th>
                        <th>Ticket Text</th>
                        <th>Created at </th>
                    </tr>
                    <tr th:each="ticket : ${ticket}">
                        <td th:text="${ticket.ticket_station}">Text ...</td>
                        <td th:text="${ticket.ticket_text}">Text ...</td>
                        <td th:text="${ticket.ticket_created_at}">Text ... 
   </td>
                    </tr>
                </table>
            </div>
   </body>
  </html> 

应用程序属性:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/dol
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

架构:

   CREATE DATABASE dol;
   CREATE TABLE `dol`.`ticket` (
     id INT NOT NULL AUTO_INCREMENT,
     ticket_text VARCHAR(100) NOT NULL,
     ticket_created_at DATETIME NOT NULL,
     ticket_station VARCHAR(250) NOT NULL,
     PRIMARY KEY (id));

   INSERT INTO `dol`.`ticket` ( `ticket_text`, `ticket_station`, 
   `ticket_created_at`) VALUES ( 'wow', 'London', '2018-01-13 15:59:26');

Pom.xml:

   <?xml version="1.0" encoding="UTF-8"?>
<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 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.pstw</groupId>
<artifactId>Portal</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Portal</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</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-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>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • 也许您应该查看localhost:8080/index 而不是localhost:8080
  • 我使用了 localhost:8080 并且它仍然有效。但不知何故,数据库中的数据没有出来。(localhost:8080/index,我尝试它显示错误whitelable错误页面)。
  • @Wero 你的类路径中有百里香吗?
  • @SangamBelose 我想我没有类路径。
  • 您使用的是什么构建工具?如何指代弹簧罐..?检查你的依赖项中是否有 thymeleaf jar

标签: mysql spring thymeleaf


【解决方案1】:

不确定 Thymeleaf 是否可以处理 Iterable。尝试在控制器或服务中传递一个简单的 Arraylist。

Ticket ticket = new Ticket();
ticket.setTicket....
List<Ticket> tickets = new ArrayList<>(ticket);
model.addAttribute("ticket", tickets);

如果可行,请在您的服务中将 Iterable 更改为 List 并重试。也许你的 MySql 数据没有问题

【讨论】:

    猜你喜欢
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多