[英]doubt about implementing search facility in playframework
I have a web app which has 3 page views. 我有一个具有3个页面浏览量的Web应用程序。
1.an index
page where all Item
s in db are listed ,and details of only the Item
with latest creationDate
are shown. 1.一个index
页面,其中列出了db中的所有Item
,并且仅显示了具有最新creationDate
的Item
详细信息。
2.An itemDetails
page where details of a selected item are shown.This page also lists all the items in db. 2.一个itemDetails
页面,其中显示所选项目的详细信息。该页面还列出了db中的所有项目。
Clicking on a listed item will bring up the itemDetails page. 单击列出的项目将调出itemDetails页面。
(These two pages also contain a text input field ,where user can enter a word to search for items matching names.) (这两个页面还包含一个文本输入字段,用户可以在其中输入单词来搜索与名称匹配的项目。)
3.A search
results page,which shows a listing of items that match a search query. 3. search
结果页面,其中显示与搜索查询匹配的项目列表。
I implemented the the first two pages and created the static public functions.Now,I want to implement the search.I created the function and a results page which lists the search results. 我实现了前两个页面并创建了静态公共函数。现在,我想实现搜索。我创建了函数和一个列出搜索结果的结果页面。
My problem is that ,if I click on an item from search results,it will bring up the itemDetails page which shows details of that item and a listing of all items in db
,since that is how the itemDetails function was implemented.I would rather have the click take me to a page, which shows the details of item and items which were results of the search query
. 我的问题是,如果我从搜索结果中单击某个项目,它将弹出itemDetails页面,该页面显示该项目的详细信息and a listing of all items in db
,因为这是实现itemDetails函数的方式。我宁愿单击将我带到一个页面,该页面显示项目的详细信息and items which were results of the search query
。 I know it is not possible in the above implementaion since state is not retained between two requests. 我知道在上述实现中是不可能的,因为在两个请求之间没有保留状态。
So,how should I do this?I cannot think clearly about this.Can you shed some light? 所以,我应该怎么做?我不能对此清楚地考虑。你能阐明一点吗?
The code is like 代码就像
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 page is 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 page: itemDetails.html页面:
is similar to the index page ..for the time being I have copied all the contents of index page . 类似于索引页..暂时复制了索引页的所有内容。
search page: 搜索页面:
#{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}
I see a couple possibilities. 我看到了几种可能性。 First you could add a new public static void itemSearchDetails(Long id, String keyword)
action to your controller. 首先,您可以向控制器添加一个新的public static void itemSearchDetails(Long id, String keyword)
操作。 Then add a new itemSearchDetails.html page. 然后添加一个新的itemSearchDetails.html页面。 Finally change the link in the search.html page to <a href="@{Application.itemSearchDetails(item.id)}">${item.name}</a>
最后,将search.html页面中的链接更改为<a href="@{Application.itemSearchDetails(item.id)}">${item.name}</a>
Here is the approach I would take. 这是我要采取的方法。 Modify the itemDetails() method to include a keyword parameter. 修改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);
}
Then change the corresponding calls in the HTML files. 然后在HTML文件中更改相应的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.