【问题标题】:Passing list<object> to thymeleaf with Spring boot使用 Spring Boot 将 list<object> 传递给 thymeleaf
【发布时间】:2017-10-28 18:54:56
【问题描述】:

我有部分控制器:

@RequestMapping(value = "1", method = RequestMethod.GET)
public String greeting(@RequestParam(value="1", required = false, defaultValue = "1") int id, Model model){

    List<Question> survey = surveyDAO.getSurveyById(id);
    model.addAttribute("survey", survey);
    return "showSurvey";
}

我尝试在其中添加属性列表 以传递到 thymeleaf 页面。但在百里香方面,我只收到“无法解决”错误。

showSurvey.html 中的部分 .html

<div class="col-sm-12">
    <table class="table table-hover">
        <tr th:each="survey: ${survey}">
            <td th:text="${survey">1</td>
            <!-- <td><a href="#" th:text="${message.name}">Title ...</a></td>-->
        </tr>
    </table>
</div>

使用 mvn package 打包然后运行 ​​jar 会启动应用程序,它可以工作,但在尝试解决 showSurvey.html 上的“调查”时崩溃,因此 SurveyDAO.GetSurveyById(id); 实际上会正常返回。

那么我应该如何传递列表然后从中显示正确的值?它有两个 int 和 String 。

【问题讨论】:

  • &lt;td th:text="${survey"&gt;1&lt;/td&gt; 看起来不对。它必须是这样的&lt;td th:text="${survey.id}"&gt;1&lt;/td&gt;
  • 哦,是的,你是对的!虽然我只是把它拿出来尝试只使用对象,但结果是一样的,thymeleaf 根本无法解析传入的列表。
  • 请同时发布堆栈跟踪。
  • 是的,你是对的。原来这只是一个错字...

标签: java spring maven spring-boot thymeleaf


【解决方案1】:

就像我在 cmets 中所说,您的代码看起来不正确。

像这样修复你的代码:

<div class="col-sm-12">
    <table class="table table-hover">
        <tr th:each="survey: ${survey}">
            <td th:text="${survey.id}">1</td> // id is a property of the Question class
        </tr>
    </table>
</div>

【讨论】:

    【解决方案2】:

    您缺少正确的“}” - 我刚刚对此进行了测试,它工作正常:

    <table>
        <tr th:each="survey : ${survey}">
            <td th:text="${survey}">1</td>
        </tr>
    </table>
    

    只是一个错字。

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2017-03-15
      • 2019-04-14
      • 2016-03-02
      • 1970-01-01
      相关资源
      最近更新 更多