Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JSP页面上显示列表_Jsp_Spring Mvc - Fatal编程技术网

在JSP页面上显示列表

在JSP页面上显示列表,jsp,spring-mvc,Jsp,Spring Mvc,我一直在开发一个web应用程序,需要在JSP上显示与我创建的完整响应类相关的值。FullResponse类在下面 public class FullResponse { Double total_hits; Double max_score; List<Hits> hits; public FullResponse(){ } public Double getTotal_hits() { return total

我一直在开发一个web应用程序,需要在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;
    }

}
fields类如下所示

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个点击的名称“products”。使用这些点击,我需要显示产品的名称和一个按钮,以根据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>  

冰箱里有什么?
产品
${hit.fields.name}

今天是我第一次使用这个网站,所以我只是在学习向上投票和其他功能,但我会确保做到这一点。我让它在结果中循环,但是我不能让它的名字出现。我试过几个不同的电话,但名字一直没有出现。我知道它是循环的,因为我用按钮将表中的6行显示出来。

请查看
forEach
这里:

对循环中选择的当前变量使用
var
,对列表使用
items
。 例:

${hit.name}//打印名称
不要忘记导入标记库:

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


在命名类属性、getter和setter时尽量遵循Java约定,使用JSP页面和bean会更容易。

注意,您正在使用key
products
覆盖属性。我想我对您在处理程序方法中使用key“products”覆盖属性的说法感到困惑,您正在执行
model.addAttribute(“产品”,…
有两个不同的对象,第二个覆盖了第一个。好的,我明白你的意思,但我仍然无法显示名称,这是我需要做的最后一件事。我仍然无法引用每个属性,因为每个类中都有列表,这会让我感到不适。我会检查该字段是否正确lly在控制器处理程序方法中调试时具有一个值。或者使用记录器输出该值并查看它是否为空。
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>