2011-08-26 3 views
0

3 페이지 뷰가있는 웹 앱이 있습니다.playframework에 검색 기능을 구현하는 것에 대해 의문이있다.

1.an indexdB의 모든 Item의이 나열되는 페이지 만 Item 최신와 creationDate의 세부 사항이 표시됩니다.

2.An itemDetails 페이지 선택한 항목의 세부 정보가 표시됩니다.이 페이지에는 또한 db의 모든 항목이 나열됩니다.

나열된 항목을 클릭하면 세부 정보 페이지가 나타납니다.

는 (이 두 페이지는 사용자가 항목 이름을 일치하는 검색 할 단어를 입력 할 수있는 텍스트 입력 필드가 포함되어 있습니다.) 검색과 일치하는 항목의 목록을 보여줍니다

3.A search 결과 페이지를, 질문.

첫 번째 두 페이지를 구현하고 정적 공용 함수를 만들었습니다. 이제 검색을 구현하고 싶습니다. 검색 결과를 나열하는 함수와 결과 페이지를 만들었습니다.

내 문제는 검색 결과에서 항목을 클릭하면 해당 항목 and a listing of all items in db의 세부 정보를 표시하는 itemDetails 페이지가 표시됩니다. 그 이유는 itemDetails 기능이 구현 된 방식이기 때문입니다. 항목 and items which were results of the search query의 세부 사항을 보여주는 페이지로 이동하십시오. 상태가 두 요청 사이에 유지되지 않으므로 위의 구현에서 가능하지 않다는 것을 알고 있습니다.

내가 어떻게해야합니까? 나는 이것에 대해 명확하게 생각할 수 없습니다. 약간의 빛을 줄 수 있습니까?

내가 가진 당분간 ..for 인덱스 페이지와 유사하다 : index.html 페이지가

#{extends 'main.html' /} 
#{set title:'Home' /} 
#{if item} 
    //show the details of item 
#{/if} 
#{if items.size()>0} 
    #{list items:items, as:'item'} 
     <a href="@{Application.itemDetails(item.id)}">${item.name}</a> 
    #{/list} 
#{/if} 
#{else} 
     There are currently no items   
#{/else} 
#{form @Application.search(keyword)} 
    <input type="text" name="keyword" id="keyword" size="18" value=""/> 
    <input type="submit" value="search"/> 
#{/form} 

itemDetails.html 페이지입니다

package controllers; 
... 
public class Application extends Controller { 
    ... 
    public static void index() { 
     //all items 
     List<Item> items = Item.find("order by creationDate desc").fetch(); 
     //the latest created item 
     Item item = items.get(0);  
     render(items,item); 
    } 

    public static void itemDetails(Long id) { 
     //selected item  
     Item item = Item.findById(id); 
     //all items 
     List<item> items = Item.find("order by creationDate desc").fetch(); 
     render(items,item); 
    } 
    public static void search(String keyword) { 
     String kw = keyword.trim(); 
     String pattern = "%"+kw+"%"; 
     String query="select distinct item from Item item where item.name like :pattern"; 
     List<Item> items = Item.find(query).bind("pattern", pattern).fetch(); 
     render(items); 
    } 
... 
} 

처럼

코드는 색인 페이지의 모든 내용을 복사했습니다.

검색 페이지 : 나는 몇 가지 가능성을 참조

#{extends 'main.html' /} 
#{set title:'search results' /} 

#{if items.size()>0} 
    #{list items:items, as:'item'} 
     <a href="@{Application.itemDetails(item.id)}">${item.name}</a> 
    #{/list} 
#{/if} 

#{else} 
    <div class="empty"> 
     could not find items with matching name here. 
    </div> 
#{/else} 

답변

1

. 먼저 컨트롤러에 새로운 public static void itemSearchDetails(Long id, String keyword) 액션을 추가 할 수 있습니다. 그런 다음 새 itemSearchDetails.html 페이지를 추가하십시오. 마지막으로 search.html 페이지의 링크를 <a href="@{Application.itemSearchDetails(item.id)}">${item.name}</a>으로 변경하십시오.

여기 제가 취할 접근법입니다. itemDetails() 메서드를 수정하여 키워드 매개 변수를 포함시킵니다.

public static void itemDetails(Long id, String keyword) { 
    //selected item  
    Item item = Item.findById(id); 
    List<item> items; 
    if (StringUtils.isNotBlank(keyword)) { 
     String query="select distinct item from Item item where item.name like :pattern"; 
     items = Item.find(query).bind("pattern", pattern).fetch(); 
    } else { 
     items = Item.find("order by creationDate desc").fetch(); 
    } 
    render(items,item); 
} 

그런 다음 HTML 파일에서 해당 호출을 변경하십시오.

+0

'키워드'는 검색 결과 페이지로 전송되어야합니다. – Ryan

관련 문제