简体   繁体   English

Springboot + thymeleaf在html表中显示数据库内容

[英]Springboot + thymeleaf displaying database contents in html table

I'm trying to learn how to make a web app using springboot and thymeleaf. 我正在尝试学习如何使用springboot和thymeleaf制作Web应用程序。 As an exercise I want to display two columns from a random database table I have created in mysql (persons) in a simple html table. 作为练习,我想显示一个我在mysql(人)中创建的随机数据库表的两列,它们是一个简单的html表。

I've used a couple of tutorials for this and wrote the code below however my html does not display the contents of the database only the table header. 我为此使用了一些教程,并在下面编写了代码,但是我的html只显示表头而不显示数据库的内容。 I have absolutely no idea where I got it wrong. 我完全不知道我哪里弄错了。 I looked up other questions here and they were all using something called jpa. 我在这里查找了其他问题,他们都使用了称为jpa的东西。 Is that better than my approach? 那比我的方法好吗? If so where can I find a begginer's tutorial. 如果是这样,我在哪里可以找到入门指南。

Code

The app class 应用程式类别

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);
    }

}

The controller 控制器

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";
    }
}

Service class 服务等级

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);
    }

And the model class 和模特班

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 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>

You have bind Users in variable user in modelAttribute . 您已在modelAttribute变量user中绑定了Users。 Try to bind as users cause in HTML you used users as list 尝试以users身份进行绑定,因为您使用users作为列表的HTML

md.addAttribute("users", service.findAll());
return "user";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SpringBoot + Thymeleaf + SQL,显示来自数据库的 HTML 表 - SpringBoot + Thymeleaf + SQL, display HTML table from database 通过springboot和thymeleaf将对象从数据库列出到html页面 - Listing objects from database to html page via springboot and thymeleaf Springboot和Thymeleaf:HTML-&gt; PDF转换 - Springboot & Thymeleaf : HTML - > PDF conversion 图像未在 html thymeleaf springboot 中显示 - image is not showing in html thymeleaf springboot Thymeleaf表未在Spring Boot应用程序的html页面上显示 - Thymeleaf table not displaying on html page in Spring Boot app 为什么我的数据库 (mysql) 中的信息没有显示在我的 html 模板上? 这是一个使用 thymeleaf 的 springboot 应用程序 - Why is the information from my database (mysql) not showing on my html template? This is a springboot application using thymeleaf 使用springboot上传图片到数据库,jpa,thymeleaf - uploading a image to the database using springboot , jpa ,thymeleaf 将 HTML 转换为 -&gt; PDF -&gt; 转换为 MULTIPARTFILE SpringBoot 和 Thymeleaf - Convert HTML to -> PDF ->to MULTIPARTFILE SpringBoot and Thymeleaf Thymeleaf SAXParseException:不显示HTML页面 - Thymeleaf SAXParseException: not displaying HTML page 等价的 <html:messages> 在百里香叶中显示消息 - equivalant <html:messages> in thymeleaf for displaying messages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM