与Thymeleaf的纠缠

时间:2019-04-19 18:33:59

标签: java spring-boot thymeleaf

我正在尝试使用Spring-Boot,Thymeleaf和Hibernate来开发简单的Web应用程序。 Hibernate运作良好:列表会话不为空。

胸腺不显示我的观点。我是初学者,无法解决此问题,请帮忙。

@Controller
public class IndexController {

    @Autowired
    FormSessionRepository formSessionRepository;

    @RequestMapping("/")
    public ModelAndView index() {
        ModelAndView index = new ModelAndView("index");
        List<FormSession> sessions = formSessionRepository.findAll();
        index.addObject("sessions", sessions);
        return index;

    }

}

<!DOCTYPE HTML>
<html th="http://thymeleaf.org" xmlns:th="http://java.sun.com/xml/ns/persistence/orm">
<header><title>Home Page</title></header>
<link href="button.css" rel="stylesheet">
<body>
<div class="wrapper">
    <a href="#" class="button"/>GET RESPONSE</a>
    <a href="#" class="button"/>GET RESPONSE</a>
    <a href="#" class="button"/>GET RESPONSE</a>
</div>
<table>
    <tr th:each="session : ${sessions}">
        <td th:text="${session.getId}">1</td>
    </tr>
</table>

</body>
</html>

错误stackTrace:

There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/index.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/index.html]")
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
    at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)
    at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)

2 个答案:

答案 0 :(得分:0)

尝试将您在Controller中的代码更改为:

@RequestMapping("/")
public ModelAndView index(Model model) {
    ModelAndView index = new ModelAndView("index");
    List<FormSession> sessions = formSessionRepository.findAll();
    model.addAttribute("sessions", sessions);
    return index;
}

在您使用Thymeleaf时,可能足以使用:

@RequestMapping("/")
public String index(Model model) {
    List<FormSession> sessions = formSessionRepository.findAll();
    model.addAttribute("sessions", sessions);
    return "index";
}

答案 1 :(得分:0)

我有解决方法!这很容易。 “会话”是一个保留字,当我将代码更改为此时,它会很好地工作。

<table>
    <tr th:each="formsession : ${sessions}">
        <td th:text="${formsession.id}">1</td>
    </tr>
</table>
相关问题