首页 > 解决方案 > 我不能列出报告,我不收集数据

问题描述

我正在尝试在 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);

标签: javaspringspring-bootthymeleaf

解决方案


感谢 Ayrtonn,我的问题在于 Thymeleaf 无法解析表达式

@{/addReport()

推荐阅读