Search code examples
hibernatejspstruts2displayobject

Trying to display data fetched from database on a jsp page (I am using Struts 2 with Hibernate)


I am trying to display data from my database to a Jsp page using Hibernate with Struts 2. Although there is no error nothing gets displayed.

I tried to find a solution from other posts but could not.

This the action class.

public class viewDayReportAction extends ActionSupport {

private static final long serialVersionUID = 9149826260758390091L;

//DayReportPersistence is the class which maps the table in hibernate
private List<DayReportPersistence> dayReportsList;
public List<DayReportPersistence> getDayReportsList() {
    return dayReportsList;
}
public void setDayReportsList(List<DayReportPersistence> dayReportsList) {
    this.dayReportsList = dayReportsList;
}

private DayReportPersistence dayReport;
public DayReportPersistence getDayReport() {
    return dayReport;
}
public void setDayReport(DayReportPersistence dayReport) {
    this.dayReport = dayReport;
}

private DayReportQuery dayReportQuery;
public viewDayReportAction() {
    dayReportQuery=new DayReportQuery();
}

@Override
public String execute(){ 
    this.dayReportsList=dayReportQuery.list();
    System.out.println(dayReportsList);
    //this statement prints the following in the tomcat output screen
    //[Hibernate.DayReportPersistence@1974acd, Hibernate.DayReportPersistence@1afe8ef,
    //Hibernate.DayReportPersistence@1974acd, Hibernate.DayReportPersistence@1afe8ef]
    //there are four objects because 4 records were fetched from the table

    return "success";
}
}

This is dayReportQuery.java which executes the query and returns result

public class DayReportQuery {

public List<DayReportPersistence> list(){
    Session session = HibernateConnection.getSessionFactory().getCurrentSession();
    session.beginTransaction();
    List<DayReportPersistence> dayReportsList = null;
    try {
        dayReportsList = (List<DayReportPersistence>) session.createQuery(
            "from DayReportPersistence").list();
        session.getTransaction().commit();

    } catch (HibernateException e) {
        System.out.println(e.getMessage());
        session.getTransaction().rollback();

    }
    return dayReportsList;
}
}

And this is my jsp page which should display the data

<table>
<s:iterator value="dayReportsList" var="dayReport">
    <tr>
        <td><s:property value="Customer_Code"/></td>
        <td><s:property value="Contact_Code"/></td>
        <td><s:property value="Employee_Code"/></td>
        <td><s:property value="Enquiry_Code"/></td>
        <td><s:property value="Stage"/></td>
        <td><s:property value="Type"/></td>
        <td><s:property value="Date"/></td>
    </tr>
</s:iterator>
</table>

I don't understand what am I doing wrong.


Solution

  • Make sure that your DayReportPersistence class have the correct getters and setters for the fields you are looking for (like Customer_Code, Contact_code, etc.)

    Also, I noticed that these are capitalized with underscores which is not typically the case for Java variables.

    <s:property value="Customer_Code"/> should be <s:property value="customerCode"/>

    and the getter in the DayReportPersistence classwould be getCustomerCode().

    Edit: Capitalization does matter. If your getter is getContact_Code() then you should refer to it as <s:property value="contact_Code"/>. Note the lower case letter 'c' in contact_Code