2011-08-26 57 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