尝试在 jsp 页面上显示从数据库获取的数据(我使用 Struts 2 和 Hibernate)

发布于 2024-11-27 08:14:39 字数 2747 浏览 2 评论 0原文

我正在尝试使用 Hibernate 和 Struts 2 将数据库中的数据显示到 Jsp 页面。虽然没有错误,但没有显示任何内容。

我试图从其他帖子中找到解决方案,但找不到。

这是动作类。

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";
}
}

这是 dayReportQuery.java 执行查询并返回结果

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;
}
}

这是我的 jsp 页面,应该显示数据

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帥小哥 2024-12-04 08:14:39

确保您的 DayReportPersistence 类对于您要查找的字段(如 Customer_Code、Contact_code 等)具有正确的 getter 和 setter。

此外,我注意到这些下划线大写,这对于 Java 变量来说通常不是这种情况。

应该是

并且 DayReportPersistence 类中的 getter 应该是getCustomerCode()

编辑:
大小写确实很重要。如果您的 getter 是 getContact_Code(),那么您应该将其称为 。请注意 contact_Code 中的小写字母“c”

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

滿滿的愛 2024-12-04 08:14:39

除了命名约定之外,我可能建议在使用操作类的 Preparable 实现触发执行方法之前设置列表。如果一个字段命名不正确,它将是空白的,如果列表不为空或为空,您仍然应该在 jsp 上得到一些内容。

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.dayReportsList=dayReportQuery.list();从执行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文