2014-03-30 89 views
3

我試圖創建一個搜索模塊,基於在選擇的標準數據庫搜索商店的細節,用戶選擇選擇標籤總是返回null

index.scala.html

@import helper._ 
@import helper.twitterBootstrap._ 

@main(Html("Home")) { 
    <!-- Main component for a primary marketing message or call to action --> 
    <div class="jumbotron"> 
    <h1>Shop Directory</h1> 
    <p>Lets you search a nearby Shop and get to know their location</p> 
    <p>Search Shop by Product or Shop name</p> 

    <form class="form-inline" action="@routes.SearchController.search()" method="post"> 
     <input type="text" class="form-inline input-lg" placeholder="Product/Shop name" name="keyword" required="keyword required"> 

     <select class="form-inline input-lg" id="Select1" name="criteria"> 
     <option value="">-:Select Criteria:-&nbsp;</option> 
     <option value="shop">Shop</option> 
     <option value="product">Product</option> 
     </select> 
     <button class="btn btn-lg btn-primary" role="button">Search</button> 
    </form> 
    </div> 
} 

Search.java

package viewmodels; 

public class Search { 
    public String keyword; 
    public String criteria; 
} 

SearchResult.java

package viewmodels; 

import models.Shop; 
import play.mvc.Controller; 
import java.util.ArrayList; 
import java.util.List; 

public class SearchResult extends Controller { 
    public String criteria; 
    public String keyword; 

    public List<Shop> shops; 

    public SearchResult() { 
    shops = new ArrayList(); 
    } 
} 

SearchController.java

package controllers; 

import models.Product; 
import models.Shop; 
import play.data.DynamicForm; 
import play.data.Form; 
import play.mvc.Controller; 
import viewmodels.Search; 
import viewmodels.SearchResult; 
import java.util.List; 
import play.mvc.Result; 
import static play.data.Form.*; 

public class SearchController extends Controller { 

    public static Result search() { 

    Form<Search> requestData = form(Search.class).bindFromRequest(); 

    Search datatosearch = requestData.get(); 
    // String criteria="shop"; 
    String criteria = datatosearch.criteria; 

    SearchResult result = new SearchResult(); 
    result.criteria = criteria; 
    result.keyword = datatosearch.keyword; 

    if (criteria == "shop") { 
     List<Shop> shops = Shop.findByShopName(datatosearch.keyword); 
     result.shops.addAll(shops); 
    } 
    else if (criteria == "product") { 
     List<Shop> shops = Product.findByShopName(datatosearch.keyword); 
     result.shops.addAll(shops); 
    } 

    return ok(views.html.search.results.render(result)); 
    } 
} 

,如果我在SearchController.java然後做String criteria="shop"String criteria="product"它工作正常,這意味着我的模型的查詢是正確的,但如果我執行上面的代碼與字符串criteria = datatosearch.criteria它顯示一個空白屏幕。

我正在使用玩框架,我真的堅持在這和任何幫助將不勝感激。

+1

你是什麼意思它返回null?你在哪裏訪問它在上面的代碼? –

+0

感謝您的回覆,我的錯誤,我已編輯我的問題,現在看到上面 – akku

回答

1

您正在比較字符串與==運算符,這是一個否定的。將字符串比較更改爲使用String.equals,以便實際比較值而不是對象引用。

if (criteria.equals("shop") { 
    ... 
} 
else if (criteria.equals("product") { 
    ... 
} 

你可能也想添加一些驗證檢查criteriaNULL

+0

感謝您的幫助@estmatic – akku