java - 我不能列出报告,我不收集数据
问题描述
我正在尝试在 springboot 中使用搜索引擎进行简单列表,但无法生成报告列表。当我尝试在视图报告中获取信息时,我没有得到对象的信息。
如果我尝试在控制器中使用
model.addAttribute("reports", reportService.findAll();
org.thymeleaf.exceptions.TemplateInputException:模板解析时出错(模板:“类路径资源[templates/views/listReport.html]”)
如果我在控制器中使用
model.addAttribute("reports", reportService.findByTitle(title));
我什么也没有收到。
看法:
<div layout:fragment="content" class="container sandstone">
<form action="/report" class="form-inline">
<div class="form-group mb-2">
<input type="text" class="form-control" name="name" placeholder="Search Title" />
<input type="submit" value="Search" class="btn btn-primary"/>
</div>
</form>
<div class="card">
<div class="card card-body">
<table class="table table-hover">
<thead>
<tr>
<th>Date</th>
<th>Title</th>
<th>Link</th>
<th>Description</th>
<th>Add</th>
</tr>
</thead>
<tbody>
<tr th:each="report:${reports}">
<td th:text="${report.date}"></td>
<td th:text="${report.title}"></td>
<td th:text="${report.link}"></td>
<td th:text="${report.description}"></td>
<td><a th:href="@{/addReport()}" class="btn btn-dark">AddReport</a></td>
</tr>
</tbody>
</table>
</div>
</div>
控制器:
@Controller
public class ReportController {
@Autowired
ReportService reportService;
@GetMapping("/report")
public String listReports(Model model, @RequestParam(defaultValue="") String title) {
model.addAttribute("report", reportService.findByTitle(title));
return "views/listReport";
}
@GetMapping("/addReport")
public String reportForm(Model model) {
model.addAttribute("report", new Report());
return "views/reportForm";
}
}
服务:
@Service
public class ReportService {
@Autowired
private ReportRepository reportRepository;
public void createReport(Report report) {
report.setTitle(report.getTitle());
report.setDate(report.getDate());
report.setDescription(report.getDescription());
report.setLink(report.getLink());
reportRepository.save(report);
}
public Report findOne(Long id) {
return reportRepository.findOne(id);
}
public List<Report> findAll() {
return reportRepository.findAll();
}
public List<Report> findByTitle(String title) {
return reportRepository.findByTitleLike("%"+title+"%");
}
}
存储库:
public interface ReportRepository extends JpaRepository<Report, Long>{
List<Report> findByTitleLike(String string);
解决方案
感谢 Ayrtonn,我的问题在于 Thymeleaf 无法解析表达式
@{/addReport()
推荐阅读
- typescript - 打字稿:isNaN 不接受日期对象
- discrete-mathematics - 图灵机从 3 个字符的字母表中接受字符串
- python - 在 Windows 10 上安装 Python 3.7 的路径时遇到问题
- pandas - 如何处理熊猫无法识别的空单元格?
- matlab - 我可以从 fminsearch 获得雅可比行列式吗
- java - 使用@Embedded 类的“字段列表”中的未知列
- regex - 比赛后负前瞻
- pip - 如何在 `pip-9.03` 和 `python-2.6` 中修复此错误
- r - 为堆积面积图指定颜色
- java - OnClickListener 未在回收站视图中调用