JSP没有重新实例化

时间:2016-09-02 09:03:05

标签: java arrays jsp

这是我想要制作的在线图书馆网站。对于可以接受多个输入的高级搜索,我从一系列所有出版物开始,然后删除与用户输入不匹配的项目。这在第一次正常工作,其中数组缩短为正确的数组。因此,如果有人输入“编程”,则将显示所有在其标题中具有编程的出版物。现在,如果用户再次单击NavBar上的“高级搜索”按钮(如welcome.jsp中所示)并且输入说“UML”,我的代码将在缩短的数组上执行搜索UML,其中包含仅用于“编程”的结果”。我在上面放了一份印刷声明

SearchResult sr = new SearchResult(); sr.searchContains = publications;

(在控制servlet中)它打印输入UML和编程,但似乎要么不创建包含所有发布的新数组?

searchResult.jsp:

<c:forEach var="current" items="${sr.searchContains}"> Title: ${current.title} </c:forEach>

ControllerServlet.java:

 if (action.equals("advSearch")) {
             SearchResult sr = new SearchResult();
             sr.searchContains = publications;

             nextPage = "searchResult.jsp";

             String titleString = request.getParameter("titleString");

             if (!titleString.equals("")){
                 ArrayList<Publication> toRemove = new ArrayList<Publication>();
                 for (Publication p : sr.searchContains){
                     if (!p.title.contains(titleString)){
                         toRemove.add(p);
                     } 
                 } 
                 sr.searchContains.removeAll(toRemove);

             }
 request.getSession().setAttribute("sr", sr);
             RequestDispatcher rd = request.getRequestDispatcher("/"+searchResult.jsp);
             rd.forward(request, response);

的welcome.jsp:

<ul class="nav navbar-nav">
        <li class="active"><a href="advancedSearch.jsp">Advance Search <span class="sr-only">(current)</span></a></li>
        </ul>

advancedSearch.jsp:

<form action="controller" method="POST">
  <div class="form-group">
     <label for="title">Title:</label>
     <input type="text" class="form-control" name="titleString">
     <input type="hidden" name="action" value="advSearch" />
  </div>
 <button type="submit" class="btn btn-default">Advanced Search</button>
</form>

SearchResult.jsp:

public class SearchResult {
    public ArrayList<Publication> searchContains = new ArrayList<Publication>();

    public ArrayList<Publication> getSearchContains (){
        return this.searchContains;
    }
}

1 个答案:

答案 0 :(得分:0)

找到解决方案,问题出在controlservlet

SearchResult sr = new SearchResult();
sr.searchContains = publications;

这意味着sr.searchContains与出版物(主数组)的引用相同。