3

I'm trying to learn how to make a web app using springboot and thymeleaf. 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.

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. I have absolutely no idea where I got it wrong. I looked up other questions here and they were all using something called 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

<!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>
2
  • 1
    provide your html code where you bind the data Commented Aug 24, 2017 at 7:40
  • Added the html code Commented Aug 24, 2017 at 7:44

1 Answer 1

3

You have bind Users in variable user in modelAttribute. Try to bind as users cause in HTML you used users as list

md.addAttribute("users", service.findAll());
return "user";
Sign up to request clarification or add additional context in comments.

2 Comments

Doing that throws a 500 error in my html with the following root causes org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [Templates/user.html]"). . Exception evaluating SpringEL expression: "user.name" (template: "user" - line 15, col 13). . Exception evaluating SpringEL expression: "user.name" (template: "user" - line 15, col 13). . Property or field 'name' cannot be found on object of type 'ro.database.jdbcTest.model.Users' - maybe not public?
Making all fields public got rid of the 500 error. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.