你好我想显示一条消息和日期列表但不知道如何,我已经找到了一些代码(Model和Themeleaf),但我无法正确更改它:>我的代码: 服务
@Autowired
private SpittleRepository spittleRepository;
private List<Spittle> spittles = new ArrayList<>(Arrays.asList(
new Spittle("aaaa", new Date()),
new Spittle("bbbbbb", new Date())
));
public List<Spittle> findSpittles() {
List<Spittle> spittles = new ArrayList<>();
spittleRepository.findAll().forEach(findedSpittles::add);
return findedSpittles;
}}
控制器
@Autowired
SpittleService spittleService;
@RequestMapping(method = RequestMethod.GET)
public String findSpittles(Model model) {
model.addAttribute("spittle", spittleService.findSpittles());
return "spittles";
} }
实体
@Id
private final Long id;
private final String message;
private final Date time;
private Double latitude;
private Double longitude;
public Spittle(String message, Date time, Double latitude, Double
longitude) {
this.id = null;
this.message = message;
this.time = time;
this.latitude = latitude;
this.longitude = longitude;
}
public Spittle(String message, Date time) {
this(message, time, null, null);
}
+ geters
}}
Thymeleaf
<h2>Lista spiltów</h2>
<table class="table table-striped">
<tr>
<th>Message</th>
<th>Date</th>
</tr>
<tr th:each="spittle : ${spittle}">
<td th:text="${spittle.getMessage()}"></td>
<td th:text="${spittle.getTime()}"></td>
</tr>
</table>
存储库在扩展JpaRepository
中没有任何代码答案 0 :(得分:0)
尝试
<tr th:each="spittle : ${spittles}">
<td th:text="${spittle.message}"></td>
<td th:text="${spittle.time}"></td>
</tr>
您可能需要使用date.format
来格式化日期。
答案 1 :(得分:-1)
尝试此更改,
<强>控制器强>
@Autowired
SpittleService spittleService;
@RequestMapping(method = RequestMethod.GET)
public List<Spittle> findSpittles(Model model) {
model.addAttribute("spittles", spittleService.findSpittles());
return "spittles";
} }
<强>服务强>
public List<Spittle> findSpittles() {
List<Spittle> spittles = new ArrayList<>();
spittles = spittleRepository.findAll().forEach(findedSpittles::add);
return spittles;
}}
<强> HTML 强>
<tr th:each="spittle : ${spittles}">
<td th:text="${spittle.message}"></td>
<td th:text="${spittle.time}"></td>
</tr>