2017-08-24 296 views
1

我正试图学习如何使用springboot和thymeleaf制作一个web应用程序。作为一个练习,我想从一个随机数据库表中显示两列,我在一个简单的html表格中创建了mysql(个人)。Springboot +百里香在html表中显示数据库内容

我已经使用了几个教程,并编写了下面的代码,但是我的html不显示数据库的内容只有表头。我完全不知道我错在哪里。我在这里查询了其他问题,他们都使用了一种叫做jpa的东西。这比我的方法好吗?如果是的话,我可以在哪里找到一个begginer的教程。

代码

App类

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

,你绑定数据 – Zico

+0

添加HTML代码 – Adi

回答

2

您已经modelAttribute绑定变量user用户。尝试绑定为users事业HTML您使用users作为列表

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

做,在我的HTML具有以下根抛出一个500错误提供您的html代码会导致 ' org.thymeleaf.exceptions.TemplateInputException:模板解析过程中发生错误(模板:“class path resource [Templates/user.html]”)'。 。 '评估SpringEL表达式的异常:“user.name”(template:“user” - line 15,col 13)'。 。 '评估SpringEL表达式的异常:“user.name”(template:“user” - line 15,col 13)'。 。 '在'ro.database.jdbcTest.model.Users'类型的对象上找不到属性或字段'name' - 可能不公开?' – Adi

+0

公开所有字段可以消除500错误。谢谢。 – Adi