Q:

Play Framework IF statement and FOR loop

I am new to MVC and following this link I have a search page for resulting pdf metadatas by using Solr. My if statement and for loop in html side do not work
Searching.java in models folder: 
public class Searching {

    public String q;

    public String outputTitle;
    public String outputAuthor;
    public String outputContent;
    public String outputPage;
    public String outputPath;

}

search function in 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 code works well. I can see outputs on the terminal, but my html side has problem and it shows one book many times according to size of searchList

A:

I am posting the answer explicitly even though I was able to help the OP in the chat - maybe somebody else is running into such problem but did not check the chat:
The problem is even though you have the line in the for-loop you are still using the same searched variable. What you have to do is to reinitialize the variable when entering the loop. Something like:
for (...) {
    searched = new Searching();
    searched.outputTitle = (String)results.get(i).getFirstValue("title");
    ....
    searchList.add(searched);
}

This solves the problem with the duplicates and everything is fine now.

