Java 将值从控制器传递到视图spring mvc
我对春天很陌生。从我的控制器我得到了一个列表输出Java 将值从控制器传递到视图spring mvc,java,spring-boot,spring-mvc,model-view-controller,Java,Spring Boot,Spring Mvc,Model View Controller,我对春天很陌生。从我的控制器我得到了一个列表输出 [[Article@42e72033id=1,title='Hello',description='description',author='eeee'][Article@7702e860id=2,title='Hello',description='description',author='eeee'][Article@3c2731ffid=3,title='Hello',description='description',author='ee
[[Article@42e72033id=1,title='Hello',description='description',author='eeee'][Article@7702e860id=2,title='Hello',description='description',author='eeee'][Article@3c2731ffid=3,title='Hello',description='description',author='eeee'],[Article@157e7973id=4,标题='QQQ',描述='qqqq',作者='qqqqq']]
我想在我的文章/view.html中使用标题、描述和作者,目前看起来如下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'owners')}">
<body>
<table id="vets" class="table table-striped">
<thead>
<tr>
<th style="width: 150px;">Title</th>
<th>Description</th>
<th style="width: 120px">Author</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
model.put("articles", yourListOfArticles);
标题
描述
作者
如何从列表中获取值并显示在正文部分?首先,您需要确保呈现视图的控制器正在将列表属性传递给模型。如果您提供的列表属性称为articles,则您的正文标记应该为列表配置迭代器,例如:
<tbody>
<tr th:each="article : ${articles}">
<td th:text="${article.title}"><td>
<td th:text="${article.description}"><td>
</tr>
</tbody>
首先,您需要确保呈现视图的控制器正在将列表属性传递给模型。如果您提供的列表属性称为articles,则您的body标记应该为列表配置迭代器,例如:
<tbody>
<tr th:each="article : ${articles}">
<td th:text="${article.title}"><td>
<td th:text="${article.description}"><td>
</tr>
</tbody>
mkez00的答案(+1)将解决您的问题。尽管如此,我还是想向您展示没有任何模板引擎(Thymeleaf)的相同东西,正如您所说,您对Spring非常陌生
您首先需要了解的几件事:
文章的列表
,如下所示:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'owners')}">
<body>
<table id="vets" class="table table-striped">
<thead>
<tr>
<th style="width: 150px;">Title</th>
<th>Description</th>
<th style="width: 120px">Author</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
model.put("articles", yourListOfArticles);
在视图(此处为JSP
)中,您可以执行以下操作以显示所需的表:
articles.jsp
福
标题
描述
作者
mkez00的答案(+1)将解决您的问题。尽管如此,我还是想向您展示没有任何模板引擎(Thymeleaf)的相同东西,正如您所说,您对Spring非常陌生
您首先需要了解的几件事:
[这些来自JavaEE 5教程,但仍然相关。从各种可用资源中进行您自己的研究!]
现在,对于您的问题,如果您在控制器中的模型中放置了文章的列表
,如下所示:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout (~{::body},'owners')}">
<body>
<table id="vets" class="table table-striped">
<thead>
<tr>
<th style="width: 150px;">Title</th>
<th>Description</th>
<th style="width: 120px">Author</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
model.put("articles", yourListOfArticles);
在视图(此处为JSP
)中,您可以执行以下操作以显示所需的表:
articles.jsp
福
标题
描述
作者