2011-08-26 61 views
0

我有一個具有3個頁面瀏覽量的web應用程序。關於在playframework中實現搜索工具的疑問

1.an index頁面,其中列出了所有分數db中的Item,僅顯示了Item和最新的creationDate的詳細信息。

2.An itemDetails頁面顯示所選項目的詳細信息。此頁面還列出了所有在db中的項目。

點擊列出的項目將彈出itemDetails頁面。

(這兩個頁面還包含一個文本輸入框,在這裏用戶可以輸入要搜索的項目相匹配的名字一個字。)

3.A search結果頁面,該頁面顯示匹配搜索條件的項目的列表查詢。

我實現了前兩頁並創建了靜態公共函數。現在,我想實現search.I創建了列出搜索結果的函數和結果頁面。

我的問題是,如果我點擊搜索結果中的一個項目,它會調出itemDetails頁面,顯示該項目的詳細信息and a listing of all items in db,因爲那是itemDetails函數的實現方式。我寧願點擊帶我到一個頁面,其中顯示了項目的詳細信息and items which were results of the search query。我知道在上面的實現中這是不可能的,因爲兩個請求之間不保留狀態。

那麼,我應該怎麼做呢?我不能清楚地想到這一點。你可以點亮一下嗎?

的代碼是一樣

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); 
    } 
... 
} 

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頁:

類似於..for時間的索引頁是我複製索引頁面的所有內容。

搜索頁面:

#{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