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 displ开发者_StackOverflow社区ayed.
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.
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
Naming conventions aside, I would probably suggest setting up your lists prior to the execute method being fired using the Preparable implementation of ryour action class. If one field is named incorrectly it will be blank, you should still be getting something on the jsp if the list is not empty or null.
public class viewDayReportAction extends ActionSupport implements Preparable{
private static final long serialVersionUID = 9149826260758390091L;
//DayReportPersistence is the class which maps the table in hibernate
private List<DayReportPersistence> dayReportsList;
public void prepare(){
this.dayReportsList=dayReportQuery.list();
}
...
}
This method will fire before the execute method, having the list populated in execute may be your issue. You can then remove the this.dayReportsList=dayReportQuery.list(); from execute.
精彩评论