1

我在按索引从列表中获取对象时遇到问题:

我的jsp:

    <c:choose>      

                <c:when test="${empty findAttributes}">

                    <h1 align="center">Attributes are empty</h1>

                </c:when>

                <c:otherwise>


                    <table  align="center" border="1" id="resultTable">


                            <c:forEach items="${findAttributes}" var="findAttributesVar" varStatus="status"> 

                                    <tr>    

                                    <td align="center">${findAttributesVar.status.index.formDescriptionList.status.status.institutions.nameOfInstitution}</td>

                                    <td align="center">${findAttributesVar.status.index.formDescriptionList.status.status.institutionType.typeOfInstitution}</td>

<td align="center">${findAttributesVar.status.index.formDescriptionList.daySchedule}</td>

                                    <td align="center">${findAttributesVar.status.index.formDescriptionList.workScheduale}</td>

                                    </tr>

                          </c:forEach>                                                                                                      

                    </table>

                </c:otherwise>

            </c:choose>

我正在获取属性作为列表findAttributes并尝试通过 jsp 页面中的索引获取对象,类似于 java 代码,例如:

attributesList.get(i).getFormDescriptionList().get(i).getInstitutions().getNameOfInstitution());
attributesList.get(i).getFormDescriptionList().get(i).getInstitutionType().getTypeOfInstitution();
attributesList.get(i).getParticularDate().toString());
attributesList.get(i).getFormDescriptionList().get(i).getDaySchedule());
attributesList.get(i).getFormDescriptionList().get(i).getWorkScheduale());

我的堆栈跟踪错误:

Jul 26, 2013 4:57:18 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/web] threw exception [An exception occurred processing JSP page /WEB-INF/views/search.jsp at line 141

138:                                <tr>    
139:                                
140:                                <td align="center">${findAttributesVar.particularDate}</td>
141:                                <td align="center">${findAttributesVar.status.index.formDescriptionList.daySchedule}</td>
142:                                <td align="center">${findAttributesVar.status.index.formDescriptionList.workScheduale}</td>
143:                                <td align="center">${findAttributesVar.status.index.formDescriptionList.rotation}</td>
144:                                <td align="center">${findAttributesVar.status.index.formDescriptionList.numberOfKids}</td>


Stacktrace:] with root cause
javax.el.PropertyNotFoundException: Property 'status' not found on type edu.demidov.dto.FormDate
    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:223)
    at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:200)
    at javax.el.BeanELResolver.property(BeanELResolver.java:311)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:85)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:985)
    at org.apache.jsp.WEB_002dINF.views.search_jsp._jspx_meth_c_005fforEach_005f3(search_jsp.java:1157)
    at org.apache.jsp.WEB_002dINF.views.search_jsp._jspx_meth_c_005fotherwise_005f0(search_jsp.java:790)
    at org.apache.jsp.WEB_002dINF.views.search_jsp._jspx_meth_c_005fchoose_005f0(search_jsp.java:639)
    at org.apache.jsp.WEB_002dINF.views.search_jsp._jspService(search_jsp.java:222)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:263)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

我的 FormDate 实体:

包edu.demidov.dto;

@Entity
@Table(name="FORM_DATE")
public class FormDate implements Serializable{


    private static final long serialVersionUID = -6727012506326164775L;


    private int dateId;

        @Id
        @GeneratedValue(strategy= IDENTITY)
        @Column(name="DATE_ID")
        public int getDateId() {
            return dateId;
        }   
        public void setDateId(int dateId) {
            this.dateId = dateId;
        }


    private int version;

        @Version
        @Column(name="VERSION")
        public int getVersion() {
            return version;
        }
        public void setVersion(int version) {
            this.version = version;
        }


    private Date particularDate;

        @Column(name="PARTICULAR_DATE")
        @DateTimeFormat(pattern="yyyy-MM-dd")
        @Temporal(TemporalType.DATE)
        public Date getParticularDate() {
            return particularDate;
        }
        public void setParticularDate(Date particularDate) {
            this.particularDate = particularDate;
        }


    private List<FormDescription> formDescriptionList = new ArrayList<FormDescription>();

        @OneToMany(mappedBy="formDate")
        @Cascade(CascadeType.ALL)
        public List<FormDescription> getFormDescriptionList() {
            return formDescriptionList;
        }
        public void setFormDescriptionList(List<FormDescription> formDescriptionList) {
            this.formDescriptionList = formDescriptionList;
        }

}

c:forEach它是如何使用嵌套c:forEach在属性中的两个对象创建表的:

         -=TABLE=-
|header1|header2|header3|
-------------------------
|value1| Value1 |value1 |
-------------------------
|value2| Value2 |value2 |
-------------------------
|value1| Value1 |value1 |
-------------------------
|value2| Value2 |value2 |
4

2 回答 2

0

Java代码行:

attributesList.get(i).getFormDescriptionList().get(i).
getInstitutions().getNameOfInstitution());

在您的情况下,它的 JSTL/EL 等效项将是:

${findAttributes[status].formDescriptionList[status].institutions.nameOfInstitution}

使用括号表示法[status]

最好forEach为嵌套的List.

<c:forEach items="${findAttributes}" var="findAttributesVar">
   <c:forEach items=${findAttributesVar.formDescriptionList} var="formDescription">
      Value : ${formDescription.institutions.nameOfInstitution}
   </c:forEach> 
</c:forEach>

请记住,您在此处引用的所有 bean 类都应该根据 Java bean 规范具有公共 getter 方法。和命名约定。阅读本文以获取有关如何使用 EL 的更多信息。

于 2013-07-21T17:18:31.533 回答
0

似乎您只是想遍历一个FormDate实例列表,并在列表中显示FormDescription每个 FormDate 。所以你需要一个第一个循环:

${formDate.particularDate} 这里应该显示当前formDate的FormDescription列表

你不需要varStatus这个循环,就像在 Java 中你只需要

for (FormDate formDate : findAttributes) {
    System.out.println(formDate.getParticularDate());
    System.out.println("here should the list of FormDescription of the current formDate be displayed");
}

不需要当前迭代的索引。

要显示 FormDescriptions 列表,您需要另一个循环:

for (FormDate formDate : findAttributes) {
    System.out.println(formDate.getParticularDate());
    for (FormDescription description : formDate.getFormDescriptionList()) {
        System.out.println("    " + description.getDaySchedule());
        System.out.println("    " + description.getWorkSchedule());
    }
}

请注意,您不需要任何索引。在 JSP 中,它看起来像

<c:forEach var="formDate" items="${findAttributes}">
<tr>
    <td>${formDate.particularDate}</td>
    <td>
        <ul>
            <c:forEach var="description" items="${formDate.formDescriptionList}>
                <li>
                    ${description.daySchedule} - ${description.workSchedule}
                </li>
            </c:forEach>            
        </ul>
    </td>
</tr>

当然,布局取决于您。但是您应该了解如何使用c:forEach.

于 2013-07-21T18:13:36.003 回答