2

I have a bean that holds the results in controller and I am passing it to jsp jstl tag. I need to use JSTL to iterate over it and present the results. I am getting list from two different tables, User and Address. and then Iterating it which gives me correct output, but when I access the same object using JSTL it throws me an exception. Please take a look and let me know if I need to add anything else.

DAO Class:

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

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

The bean is getting data from two tables

  1. User (plain pojo)
  2. Address (plain pojo)

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 class

@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>

But it threw the following exception:Can Some please let me know what changes I need to make. I am stuck with this from past 2 days. Thanks in advance

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)
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Amit Agarwal
  • 191
  • 1
  • 3
  • 11

1 Answers1

1

Address is not an Iterable so can't be used as the Collection in the forEach tag. You could do

<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>
Reimeus
  • 158,255
  • 15
  • 216
  • 276