无法访问EL中的bean值

时间:2012-01-30 23:40:25

标签: jsp jstl el

public class ReportAdHoc {

    private String name;
    private String reportKind;
    private List<ReportAdHocInstance> instances;


    public void addInstance(ReportAdHocInstance instance){
        if (instances == null){
            instances = new ArrayList<ReportAdHocInstance>();
        }
        instances.add(instance);
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getReportKind() {
        return reportKind;
    }
    public void setReportKind(String reportKind) {
        this.reportKind = reportKind;
    }

    public List<ReportAdHocInstance> getInstances() {
        return instances;
    }
    public void setInstances(List<ReportAdHocInstance> instances) {
        this.instances = instances;
    }


}

public class ReportAdHocInstance {

    private int id;
    private String reportName;  
    private Date runtime;
    private String runBy;
    private String format;
    private List<ReportAdHocParameter> parameters;

    public void addParameters(ReportAdHocParameter parameter){
        if (parameters == null){
            parameters = new ArrayList<ReportAdHocParameter>();
        }
        parameters.add(parameter);
    }

    public String getReportName() {
        return reportName;
    }
    public void setReportName(String reportName) {
        this.reportName = reportName;
    }
    public Date getRuntime() {
        return runtime;
    }
    public void setRuntime(Date runtime) {
        this.runtime = runtime;
    }
    public String getFormat() {
        return format;
    }
    public void setFormat(String format) {
        this.format = format;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getRunBy() {
        return runBy;
    }
    public void setRunBy(String runBy) {
        this.runBy = runBy;
    }

    public List<ReportAdHocParameter> getParameter() {
        return parameters;
    }

    public void setParameter(List<ReportAdHocParameter> parameters) {
        this.parameters = parameters;
    }
}

public class ReportAdHocParameter {

    private int id;
    private String parameterName;   
    private String parameterValue;

    public String getParameterName() {
        return parameterName;
    }
    public void setParameterName(String parameterName) {
        this.parameterName = parameterName;
    }

    public String getParameterValue() {
        return parameterValue;
    }
    public void setParameterValue(String parameterValue) {
        this.parameterValue = parameterValue;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

}

JSP代码

<c:forEach items="${reportAdHoc.instances}" var="inst">
        <c:forEach items="${inst.parameter}" var="param">
            <td>${param.parameterName}</td>         
        </c:forEach>
</c:forEach>

我无法检索${param.parameterName}值。它显示为空白。

但是,如果我打印<td>Test</td>,则根据我的循环打印11次test,它应该包含11个值。

为什么我无法检索parameterName

1 个答案:

答案 0 :(得分:1)

${param}是一个引用请求参数映射的保留EL变量名。例如${param.foo}为您提供request.getParameter("foo")。隐式EL变量优先于自定义变量。

给它一个不同的名字。例如。 var="parameter"并使用${parameter}

<c:forEach items="${reportAdHoc.instances}" var="inst">
    <c:forEach items="${inst.parameter}" var="parameter">
        <td>${parameter.parameterName}</td>
    </c:forEach>
</c:forEach>

另见:


此外,我建议重命名这一个,以便最终得到更多自我记录代码,如下所示:

<c:forEach items="${reportAdHoc.instances}" var="instance">
    <c:forEach items="${instance.parameters}" var="parameter">
        <td>${parameter.name}</td>
    </c:forEach>
</c:forEach>