使用spring boot无法显示数据库中的数据

时间:2018-01-05 17:27:24

标签: java spring jpa spring-boot thymeleaf

我试图显示一个名为" Genres"的数据库表中的项目列表。 以下是我的代码:

GenreController:

Name 5-methyltetrahydrofolic acid
Accession Number DB04789
Type Small Molecule

GenreService

@Controller
@RequestMapping("/genre")
public class GenreController {


    private GenreService genreService;

    @Autowired
    public GenreController(GenreService albumService) {
        this.genreService = genreService;
    }


    @RequestMapping("/list")
    public String listGenre(Model model){
        model.addAttribute("genres", genreService.list());
        return "genre/list";
    }
    @RequestMapping("/views/{id}")
    public String viewAlbum(@PathVariable(value="id") String id, Model model){
        model.addAttribute("genre", genreService.getById(id));
        return "genre/view";
    }
}

GenreRepository

@Service
public class GenreService {
    GenreRepository genreRepository;

    @Autowired
    public GenreService(GenreRepository genreRepository) {
        this.genreRepository = genreRepository;
    }

    public List<Genre> list() {
        // TODO Auto-generated method stub
        return (List<Genre>) genreRepository.findAll();
    }

    public Genre getById(String id) {
        // TODO Auto-generated method stub
        return null;
    }

}

布局/ main.html中

@Repository
public interface GenreRepository extends CrudRepository<Genre, Long> {


}

流派/ list.html

<!DOCTYPE html>
<html lang="en"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Good Music</title>

    <link rel="stylesheet" href="../static/css/blog.css" th:href="@{/css/blog.css}"/>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />

    <link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css' /> 
    <link href='https://fonts.googleapis.com/css?family=Exo' rel='stylesheet' type='text/css' />

    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>

    <div class="container">

        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">Good Music</a>
                </div>
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="/">Home</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Artists <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="/artist/list">List</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Albums <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="/album/list">List</a></li>
                            </ul>
                        </li>   
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Genres <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="/genre/list">List</a></li>
                            </ul>
                        </li>                       
                    </ul>
                    <p class="navbar-text navbar-right">Signed in as <a href="#" class="navbar-link">Guest User</a></p>
                </div>
            </div>
        </nav>

        <div layout:fragment="content">
        </div>

        <footer>
            <p>Joseph Saba 2017</p>
        </footer>

    </div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" ></script>
</body>
</html>

当我去/ genre / list时,我得到一个whitelabel错误页面 当我删除<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" layout:decorator="layouts/main"> <head> <title>List Genre</title> </head> <body> <div layout:fragment="content"> <article th:each="genre : ${genres}"> <header> <h2> HELLO</h2> <h2 th:text="${genre.genreName}">Genre Name</h2> </header> <footer> <a th:href="|@{/genre/view/}${genre.genreId}|">Read More</a> </footer> <hr/> </article> </div> </body> </html> 时,我没有收到错误页面。我只是得到了main.html布局而没有任何其他内容。

编辑: 错误日志

model.addAttribute("genres", genreService.list());

1 个答案:

答案 0 :(得分:0)

我在构造函数中分配了错误的Repository。我修好了,它现在正在工作。