Thymeleaf的Spring Boot - null上下文

时间:2017-05-10 08:52:16

标签: java spring-boot thymeleaf

我的(非常基本的)jsp看起来像这样:

<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Comment proposal</title>
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
    integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
    crossorigin="anonymous" />
<!-- Latest compiled and minified JavaScript -->
<script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
    integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
    crossorigin="anonymous"></script>
    <link rel="stylesheet" href="../static/css/common.css"/>
</head>
<body>
    <div>
        <div>
            <div>
                <h1 class="text-primary text-center" th:text="${p.getTitle()}"></h1>
                <div>
                    <div>
                        <h2>Content:</h2>
                        <h3 th:text="${p.getContent()}"></h3>
                    </div>
                </div>
                <div>
                    <div>
                        <h3>Comments:</h3>
                        <div>
                            <table class="table">
                                <tr th:each="c : ${p.getComments()}">
                                    <td><div>
                                        <div class="panel panel-default">
                                            <div class="panel-heading">
                                                <span class="text-muted" th:text="${c.getUser().getName()}"></span>
                                            </div>
                                            <div class="panel-body">
                                                <p th:text="${c.getContent()}"></p>
                                            </div>
                                        </div>
                                    </div></td>
                                    <td><a th:href="${'/upvoteComment/' + p.getId()}"
                                        class="btn btn-info" th:proID="${p.getId()}">Me Likey</a></td>
                                    <td><a th:href="${'/downvoteComment/' + p.getId()}"
                                        class="btn btn-info" th:proID="${p.getId()}">Nu-uh</a></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <div th:with="idProposal=${p.getId()}">
                        <h3>Add comment</h3>
                        <form role="form" th:action="@{/createComment/} + ${idProposal}"
                            th:object="${createComment}" method="POST">
                            <textarea class="form-control" rows="3" id="contentInput"
                                th:field="*{content}" placeholder="Comment"></textarea>
                            <button value="Comment" type="submit" class="btn btn-info"
                                id="SubmitComment">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

我的控制器就是这样的:

@RequestMapping("/commentProposal/{id}")
    //move to commentProposal.html
    public String commentProposal(@PathVariable("id") String id, Model model){
        new ProposalDao();
        Proposal p = ProposalDao.GetProposalByID(Integer.parseInt(id));
        ModelAndView mav = new ModelAndView("commentProposal");
        model.addAttribute("p", p);
        mav.addObject("p", p);
        return "commentProposal";
    }

我已经尝试了返回mav对象,以及将对象添加到传递的Model中。但无济于事,每当我进入页面时,我都会得到

Exception evaluating SpringEL expression: "p.getTitle()" (commentProposal:22)

maven窗口显示“尝试在空上下文对象上调用方法getTitle()”

我一定看过无数的教程,我无法得到我做错的事。在任何前端作为一个总的菜鸟也没有帮助!

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用Spring的依赖注入来查找DAO。这可以通过自动装配(Java @Inject)来完成:

@RequestMapping("/commentProposal/{id}")
public String commentProposal(@PathVariable("id") Integer id, 
                              Model model) {
   model.addAttribute("p", proposalDao.findOne(id));
   return "commentProposal";
}

@Autowired
private ProposalDao proposalDao;

改为称呼findOne。如果您使用Spring Data,这将使您以后更容易。另请注意,Spring可以自动转换类型,因此无需解析您的Integer

更改您的HTML模板以匹配您在Thymeleaf文档中看到的内容:

<h1 class="text-primary text-center" th:text="${p.title}"></h1>

并将您的方法名称更改为驼峰大小写(按照惯例,为下一个阅读代码的人提供理智)。