不知道如何迭代<foreach> </foreach>中提供的“items”

时间:2014-12-07 15:23:57

标签: java jstl

我有一个bean将结果保存在控制器中,我将它传递给jsp jstl标签。我需要使用JSTL迭代它并呈现结果。我从两个不同的表,用户和地址获取列表。然后迭代它给我正确的输出,但是当我使用JSTL访问同一个对象时它会抛出异常。如果我需要添加任何其他内容,请查看并告诉我。

DAO班级:

String queryString_user = "from User u where u.username = :username";

Query query = session.createQuery(queryString_user).setParameter("username", user);
List<User> userDetails= query.list();

bean从两个表中获取数据

  1. 用户(普通pojo)
  2. 地址(普通pojo)
  3. User.java

    package com.application.walker.service;
    
    import javax.persistence.Entity;
    
    /**
     * User generated by hbm2java
     */
    @Entity
    public class User implements java.io.Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private Integer id;
        private Address address;
        private String firstName;
        private String lastName;
        private String username;
        private String dob;
        private String emailAddress;
        private Creditcard creditcard;
    
        public User() {
        }
    
        public User(Address address, String username, String emailAddress) {
            this.address = address;
            this.username = username;
            this.emailAddress = emailAddress;
        }
    
        public User(Address address, String firstName, String lastName,
                String username, String dob, String emailAddress,
                Creditcard creditcard) {
            this.address = address;
            this.firstName = firstName;
            this.lastName = lastName;
            this.username = username;
            this.dob = dob;
            this.emailAddress = emailAddress;
            this.creditcard = creditcard;
        }
    
        public Integer getId() {
            return this.id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public Address getAddress() {
            return this.address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
        public String getFirstName() {
            return this.firstName;
        }
    
        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }
    
        public String getLastName() {
            return this.lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getUsername() {
            return this.username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getDob() {
            return this.dob;
        }
    
        public void setDob(String dob) {
            this.dob = dob;
        }
    
        public String getEmailAddress() {
            return this.emailAddress;
        }
    
        public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
        }
    
        public Creditcard getCreditcard() {
            return this.creditcard;
        }
    
        public void setCreditcard(Creditcard creditcard) {
            this.creditcard = creditcard;
        }
    
    }
    

    Address.Java

    package com.application.walker.service;
    
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * Address generated by hbm2java
     */
    public class Address implements java.io.Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private Integer id;
        private String addressLine1;
        private String addressLine2;
        private String addressLine3;
        private String city;
        private String state;
        private Integer zipcode;
        private Set users = new HashSet(0);
    
        public Address() {
        }
    
        public Address(String addressLine1, String addressLine2,
                String addressLine3, String city, String state, Integer zipcode,
                Set users) {
            this.addressLine1 = addressLine1;
            this.addressLine2 = addressLine2;
            this.addressLine3 = addressLine3;
            this.city = city;
            this.state = state;
            this.zipcode = zipcode;
            this.users = users;
        }
    
        public Integer getId() {
            return this.id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getAddressLine1() {
            return this.addressLine1;
        }
    
        public void setAddressLine1(String addressLine1) {
            this.addressLine1 = addressLine1;
        }
    
        public String getAddressLine2() {
            return this.addressLine2;
        }
    
        public void setAddressLine2(String addressLine2) {
            this.addressLine2 = addressLine2;
        }
    
        public String getAddressLine3() {
            return this.addressLine3;
        }
    
        public void setAddressLine3(String addressLine3) {
            this.addressLine3 = addressLine3;
        }
    
        public String getCity() {
            return this.city;
        }
    
        public void setCity(String city) {
            this.city = city;
        }
    
        public String getState() {
            return this.state;
        }
    
        public void setState(String state) {
            this.state = state;
        }
    
        public Integer getZipcode() {
            return this.zipcode;
        }
    
        public void setZipcode(Integer zipcode) {
            this.zipcode = zipcode;
        }
    
        public Set getUsers() {
            return this.users;
        }
    
        public void setUsers(Set users) {
            this.users = users;
        }
    
    }
    

    Spring Controller类

    @RequestMapping(value= "/userinformation",params = "userinfo",method=RequestMethod.POST)
        private ModelAndView retrieveInformation(@RequestParam String username, HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
            User user = new User();
            ModelAndView model = new ModelAndView();
            getWalkerDaoOperations().updateUser(user);
            List user_records =getWalkerService().retireveUserbyUserName(username);
    
            if(!(user_records.isEmpty())){
            model.addObject("user_records", user_records);
            model.setViewName("userinformation");
    
            request.setAttribute("user_records", user_records);
            RequestDispatcher requestDispatcher = request.getRequestDispatcher("userinformation.jsp");
            requestDispatcher.forward(request, response);
    
            //System.out.println(requestDispatcher);
    
            }else{
                String message = "user does not exists";
                model.addObject("message", message);
                model.setViewName("searchuser");
            }
            return model;
        }
    

    JSP:

    <c:forEach var="user_records" items="${user_records}">
    
                                    <tr>
                                        <td><c:out value="${user_records.firstName}"></c:out></td>
                                        <td><c:out value="${user_records.lastName}"></c:out></td>
                                        <td><c:out value="${user_records.username}"></c:out></td>
                                        <td><c:out value="${user_records.emailAddress}"></c:out></td>
                                        <td><c:out value="${user_records.dob}"></c:out></td>
    
    
                                <c:forEach var="user_records_address" items="${user_records.address}">
                                        <td><c:out value="${user_records_address.addressLine1}"></c:out></td>
                                        <%-- <td><c:out value="${user_records_address.addressLine2}"></c:out></td>
                                        <td><c:out value="${user_records_address.addressLine3}"></c:out></td>
                                        <td><c:out value="${user_records_address.city}"></c:out></td>
                                        <td><c:out value="${user_records_address.state}"></c:out></td> --%>
                                </c:forEach> 
    
                                    </tr>
    
    
                                </c:forEach>
    

    但它引发了以下异常:有些人可以告诉我需要做出哪些改变。从过去2天开始,我就被困在这里了。提前致谢

    SEVERE: Servlet.service() for servlet [walker] in context with path [/walker] threw exception [An exception occurred processing JSP page /userinformation.jsp at line 40
    
    37:                                     <td><c:out value="${user_records.dob}"></c:out></td>
    38:                                 
    39:                             
    40:                             <c:forEach var="user_records_address" items="${user_records.address}">
    41:                                     <td><c:out value="${user_records_address.addressLine1}"></c:out></td>
    42:                                     <%-- <td><c:out value="${user_records_address.addressLine2}"></c:out></td>
    43:                                     <td><c:out value="${user_records_address.addressLine3}"></c:out></td>
    
    
    Stacktrace:] with root cause
    javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;
        at org.apache.taglibs.standard.tag.common.core.ForEachSupport.toForEachIterator(ForEachSupport.java:274)
        at org.apache.taglibs.standard.tag.common.core.ForEachSupport.supportedTypeForEachIterator(ForEachSupport.java:238)
        at org.apache.taglibs.standard.tag.common.core.ForEachSupport.prepare(ForEachSupport.java:155)
        at javax.servlet.jsp.jstl.core.LoopTagSupport.doStartTag(LoopTagSupport.java:256)
        at org.apache.jsp.userinformation_jsp._jspx_meth_c_005fforEach_005f1(userinformation_jsp.java:345)
        at org.apache.jsp.userinformation_jsp._jspx_meth_c_005fforEach_005f0(userinformation_jsp.java:209)
        at org.apache.jsp.userinformation_jsp._jspx_meth_form_005fform_005f0(userinformation_jsp.java:140)
        at org.apache.jsp.userinformation_jsp._jspService(userinformation_jsp.java:87)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
        at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
        at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
        at com.application.walker.controller.LoginController.retrieveInformation(LoginController.java:84)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at java.lang.Thread.run(Thread.java:695)
    

1 个答案:

答案 0 :(得分:1)

Address不是Iterable,因此无法用作forEach标记中的Collection。你可以做到

<c:forEach var="user_record" items="${user_records}">
   <td>${user_record.firstName}></td>
   <td>${user_record.lastName}></td>
   <td>${user_record.address.addressLine1}></td>
   <td>${user_record.address.addressLine2}></td>
   <td>${user_record.address.addressLine3}></td>
   ...
</c:forEach>