2013-12-08 127 views
0

我一直在研究Web應用程序,並需要在JSP上顯示與我創建的完全響應類相關的值。該FullResponse類是低於...在JSP頁面上顯示列表

public class FullResponse { 

    Double total_hits; 
    Double max_score; 
    List<Hits> hits; 

    public FullResponse(){ 

    } 

    public Double getTotal_hits() { 
     return total_hits; 
    } 

    public void setTotal_hits(Double total_hits) { 
     this.total_hits = total_hits; 
    } 

    public Double getMax_score() { 
     return max_score; 
    } 

    public void setMax_score(Double max_score) { 
     this.max_score = max_score; 
    } 

    public List<Hits> getHits() { 
     return hits; 
    } 

    public void setHits(List<Hits> hits) { 
     this.hits = hits; 
    } 

} 

命中等級如下表所示...

public class Hits { 

     String _index; 
     String _type; 
     String _id; 
     Double _score; 
     Fields fields; 

public Hits(){ 

     } 

public String get_index() { 
     return _index; 
    } 

    public void set_index(String _index) { 
     this._index = _index; 
    } 

    public String get_type() { 
     return _type; 
    } 

    public void set_type(String _type) { 
     this._type = _type; 
    } 

    public String get_id() { 
     return _id; 
    } 

    public void set_id(String _id) { 
     this._id = _id; 
    } 

    public Double get_score() { 
     return _score; 
    } 

    public void set_score(Double _score) { 
     this._score = _score; 
    } 

    public Fields getFields() { 
     return fields; 
    } 

    public void setFields(Fields fields) { 
     this.fields = fields; 
    } 


} 

和字段類如下...

public class Fields { 

    String item_name; 

    public Fields(){ 

    } 

    public String getName(){ 
     return item_name; 
    } 
    public void setName(String name){ 
     item_name=name; 
    } 
} 

在我的控制器我通過我的模型FullResponse,這是名稱爲「產品」,包含6個命中。使用這些匹配,我需要顯示產品的名稱和一個按鈕來選擇基於ID的產品。我如何循環訪問列表並使用相應的按鈕顯示這些名稱。我不熟悉JSP頁面,也不知道如何在命中列表中循環。有人請幫忙,這個任務是我的頭。

@RequestMapping(value = "/lookup") 
public String look(ModelMap model, 
     @RequestParam(value="find", required=true) String find, 
     HttpSession session, 
     Principal principal) throws ClientProtocolException, IOException{ 

    String user = principal.getName(); 
    model.addAttribute("products", database.getProducts(user)); 
    model.addAttribute("user", user); 

    CallSearch test=new CallSearch(find); 
    FullResponse test1 = test.search(); 
    model.addAttribute("products", test1); 

    return "productlookup"; 

「productlookup」顯然,我想顯示這個信息我的JSP頁面的名稱。我有創建頁面的核心,但我不知道如何使用這個「產品」屬性並顯示每個命中。

<table border="border" align="center"> 
    <caption>What's in the Fridge?</caption> 
    <tr> 
     <th>Product</th> 
     <th></th> 
    </tr> 
    <tr> 
     <c:forEach var="hit" items="${products.hits}"> 
      <th>${hit.fields.name}</th> 
      <th><a href="productlookup?id=${product.id}"><span class="label label-danger" >select</span></a></th> 
    </tr> 
    </c:forEach> 
</table> 

今天是我第一次使用這個網站,所以我只是學習有關upvoting和其他功能,但我會確保做到這一點。我得到它循環的結果,但我不能讓名字出現。我嘗試了幾次不同的呼叫,但名字從未出現。我知道它正在循環,因爲我用按鈕將6行放到我的桌子上。

+0

請注意,您正在用「產品」鍵覆蓋該屬性。 –

+0

我想我很困惑你在說什麼用關鍵「產品」覆蓋屬性 – user3080107

+0

在你的處理程序方法中,你正在做'model.addAttribute(「products」,...'有兩個不同的對象,第二個覆蓋第一個 –

回答

0

看看forEach這裏:jstl

使用var在環路和items的列表中選擇當前變量。 例如:

<c:forEach var="hit" items="${products.hits}"> ${hit.name} //print name </c:forEach> 

不要忘了導入的taglib:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

嘗試按照命名類屬性和getter和setter方法的Java約定,當你使用JSP頁面就容易和豆類。