簡體   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