在JSP页面上显示列表

时间:2013-12-08 20:12:50

标签: jsp spring-mvc

我一直在研究Web应用程序,需要在与我创建的Full Response类相关的JSP上显示值。 FullResponse类位于......

之下
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;
    }

}

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,其名称为“products”,包含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行到我的桌子。

1 个答案:

答案 0 :(得分:0)

在这里查看forEachjstl

对循环中选择的当前变量使用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页面和bean时会更容易。