简体   繁体   中英

Spring Boot with Thymeleaf - null context

My (very basic) jsp looks like this:

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

And my controller serves this:

@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";
    }

I've tried both returning the mav object, as well as adding the object to the passed Model. But to no avail, whenever I go on the page I get

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

And the maven window says "Attempted to call method getTitle() on null context object"

I've must've looked through countless tutorials, I just can't get what I'm doing wrong. Being a total noob in anything front end isn't helpful either!

Thanks!

You can use dependency injection for Spring to find your DAO. This can be simply done through autowiring ( @Inject for Java):

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

@Autowired
private ProposalDao proposalDao;

Call it findOne instead. This will make it easier for you later if you use Spring Data. Also note that Spring can automatically convert types, so no need to parse your Integer .

Change your HTML template to match what you see in the Thymeleaf documentation:

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

And change your method names to camel case (by convention and sanity for the next person reading your code).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM