我是MVC的新手,关注this link我有一个搜索页面,通过使用Solr得到了pdf元数据。我的if语句和html端的for循环不起作用
模型文件夹中的Searching.java:
public class Searching {
public String q;
public String outputTitle;
public String outputAuthor;
public String outputContent;
public String outputPage;
public String outputPath;
}
Application.java中的搜索功能:
final static Form<Searching> searchForm = form(Searching.class);
final static List<Searching> searchList = new ArrayList<Searching>();
public static Result search() {
Form<Searching> filledForm = searchForm.bindFromRequest();
Searching searched = filledForm.get();
....(database connection lines)
QueryResponse response = solr.query(query);
SolrDocumentList results = response.getResults();
if(results.isEmpty())
System.out.println("SEARCH NOT FOUND");
else {
for (int i = 0; i < results.size(); ++i) {
searched.outputTitle = (String)results.get(i).getFirstValue("title");
searched.outputAuthor = (String)results.get(i).getFirstValue("author");
searched.outputPage =results.get(i).getFirstValue("pageNumber").toString();
searched.outputContent = (String)results.get(i).getFirstValue("content");
searched.outputPath = (String)results.get(i).getFirstValue("path");
searchList.add(searched);
}
System.out.println("\nresults.getNumFound(): "+ searched.outputFound);
System.out.println("results.size(): "+results.size());
}
return play.mvc.Results.ok(search.render(searched, searchForm, searchList));
}
search.scala.html
@(searched: Searching, searchForm: Form[Searching], searchList: List[Searching])
.. some buttons,a search bar...
@if(searchList.isEmpty()) {
<h1>Error</h1>
} else {
@for(search <- searchList) {
<ul>Title: @search.outputTitle</ul>
<ul>Author: @search.outputAuthor <a href="@search.outputPath" download>Download PDF</a></ul>
<ul>Number of Page(s): @search.outputPage</ul>
}
}
Java代码运行良好。我可以看到终端上的输出,但是我的html端有问题,根据searchList的大小,它会多次显示一本书
答案 0 :(得分:0)
即使我能够在聊天中帮助OP,我也明确地发布了答案 - 也许其他人遇到了这样的问题,但没有检查聊天:
问题是,即使你在for循环中有一行,你仍然使用相同的searched
变量。您需要做的是在进入循环时重新初始化变量。类似的东西:
for (...) {
searched = new Searching();
searched.outputTitle = (String)results.get(i).getFirstValue("title");
....
searchList.add(searched);
}
这解决了重复的问题,现在一切都很好。