繁体   English   中英

对在Playframework中实现搜索功能的怀疑

[英]doubt about implementing search facility in playframework

我有一个具有3个页面浏览量的Web应用程序。

1.一个index页面,其中列出了db中的所有Item ,并且仅显示了具有最新creationDateItem详细信息。

2.一个itemDetails页面,其中显示所选项目的详细信息。该页面还列出了db中的所有项目。

单击列出的项目将调出itemDetails页面。

(这两个页面还包含一个文本输入字段,用户可以在其中输入单词来搜索与名称匹配的项目。)

3. search结果页面,其中显示与搜索查询匹配的项目列表。

我实现了前两个页面并创建了静态公共函数。现在,我想实现搜索。我创建了函数和一个列出搜索结果的结果页面。

我的问题是,如果我从搜索结果中单击某个项目,它将弹出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页面:

类似于索引页..暂时复制了索引页的所有内容。

搜索页面:

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

我看到了几种可能性。 首先,您可以向控制器添加一个新的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文件中更改相应的调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM