[英]SpringBoot + Thymeleaf + SQL, display HTML table from database
[英]Springboot + thymeleaf displaying database contents in html table
我正在嘗試學習如何使用springboot和thymeleaf制作Web應用程序。 作為練習,我想顯示一個我在mysql(人)中創建的隨機數據庫表的兩列,它們是一個簡單的html表。
我為此使用了一些教程,並在下面編寫了代碼,但是我的html只顯示表頭而不顯示數據庫的內容。 我完全不知道我哪里弄錯了。 我在這里查找了其他問題,他們都使用了稱為jpa的東西。 那比我的方法好嗎? 如果是這樣,我在哪里可以找到入門指南。
碼
應用程式類別
package ro.database.jdbcTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import ro.database.jdbcTest.controllers.UsersController;
@SpringBootApplication
@EnableAsync
public class App
{
@Autowired
UsersController service;
public static void main( String[] args )
{
SpringApplication.run(App.class);
}
}
控制器
package ro.database.jdbcTest.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import ro.database.jdbcTest.model.Users;
import ro.database.jdbcTest.services.UserService;
import java.util.List;
import java.util.Map;
@Controller
public class UsersController {
@Autowired
UserService service;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String index(Model md){
md.addAttribute("user", service.findAll());
return "user";
}
}
服務等級
package ro.database.jdbcTest.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import ro.database.jdbcTest.model.Users;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Service
public class UserService {
@Autowired
JdbcTemplate template;
public List<Users> findAll() {
String sql = "select * from people";
RowMapper<Users> rm = new RowMapper<Users>() {
@Override
public Users mapRow(ResultSet resultSet, int i) throws SQLException {
Users user = new Users(resultSet.getInt("id"),
resultSet.getString("name"),
resultSet.getInt("age"));
String email = resultSet.getString("email");
if (email != null) {
user.setEmail(email);
}
return user;
}
};
return template.query(sql, rm);
}
和模特班
package ro.database.jdbcTest.model;
public class Users {
private int id;
private String name;
private int age;
private String email;
public Users(int id, String name, int age){
this.id=id;
this.name=name;
this.age=age;
}
public void setEmail(String email){
this.email=email;
}
}
HTML
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>Users</h2>
<table border="1">
<tr>
<th>name</th>
<th>age</th>
</tr>
<tr th:each = "user: ${users}">
<td th:text="${user.name}">vasile</td>
<td th:text="${user.age}">45</td>
</tr>
</table>
</body>
</html>
您已在modelAttribute
變量user
中綁定了Users。 嘗試以users
身份進行綁定,因為您使用users
作為列表的HTML
md.addAttribute("users", service.findAll());
return "user";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.