我是个初学者 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();
}
服务实现:
@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();
}
}
索引.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>
1条答案
按热度按时间bqujaahr1#
我不确定thymeleaf是否能处理iterable。尝试在控制器或服务中传递一个简单的arraylist。
如果有效,请将iterable更改为在服务中列出,然后重试。也许你的mysql数据没有问题