当前位置: 首页 > 知识库问答 >
问题:

带有Thymeleaf的Spring Boot-空上下文

郗丰
2023-03-14

我的(非常基础的)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对象,以及将对象添加到传递的模型中。但无济于事,每当我进入页面时,我都会得到

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

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

我一定看了无数的教程,我就是不知道我做错了什么。做任何前端的新手也没有帮助!

谢谢!

共有1个答案

郏稳
2023-03-14

您可以使用Spring的依赖注入来找到您的DAO。这可以简单地通过自动连接来完成(对于Java为< code>@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;

请将其改为< code>findOne。如果您使用Spring数据,这将使您以后更加容易。还要注意,Spring可以自动转换类型,所以不需要解析您的< code>Integer。

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

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

并将您的方法名称更改为驼峰大小写(根据惯例和下一个阅读您的代码的人的理智)。

 类似资料:
  • 首先要说的是,我一直在寻找解决方案,现在我非常绝望。 当由Spring Boot运行时,我无法从html页面访问css文件。 html。文件 应用JAVA 文件夹结构: 我尝试过将文件夹放入文件夹和/或删除addResourcesHandler,通过相对路径引用css和其他一些东西。似乎没有什么能解决这个问题。请告诉我,如果你试图解决这个问题,但没有找到解决方案,这样我就知道,我不会被忽视。

  • 问题内容: 我的CSS和Thymeleaf有问题。 在我的Spring启动应用程序中,我具有以下结构: src / main / resource / static / css(用于CSS文件) src / main / resource / static / templates(用于html文件) 现在,使用我的Thymeleaf,将我的html页面命名为ErrorPage,将css文件命名为L

  • 我试图编写一个方法,它接受@PathVariable参数,并将用户重定向到一个jsp文件。 @pathvariable(value=“customerId”)字符串customerId @pathvariable(name=“customerId”)字符串customerId @pathvariable(“customerId”)字符串customerId 谢谢你的帮助。

  • 本文向大家介绍SpringBoot中的Thymeleaf用法,包括了SpringBoot中的Thymeleaf用法的使用技巧和注意事项,需要的朋友参考一下 Thymeleaf Thymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里。 我们为什么要用Thymeleaf来作为模板引擎呢?官网给了我们一个非常令人信服的解释: Thymeleaf is a m

  • 本文向大家介绍SpringBoot中的Thymeleaf模板,包括了SpringBoot中的Thymeleaf模板的使用技巧和注意事项,需要的朋友参考一下 一、前言     Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1、JSP 最明显的问题在于它看起来像HTML或XML,但它其实上并不是。大多数的JS

  • 我正在尝试将vaadin与spring(没有Spring Boot)和基于java注释的spring部分配置结合起来。 自动连接似乎适用于vaadin ui部分,但不适用于“自定义ui类”(例如,“公共类LoginScreen扩展自定义组件”)。我在SysOut上得到一个NPE或一个空对象。 此外,我注意到“@ComponentScan(base Packages={"net.myapp"})”没