You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Struts1整合Hibernate后JSP无法显示数据库数据求助

Struts1 + Hibernate 数据无法在JSP显示问题排查

核心问题定位

你的代码存在几处明确错误,直接导致数据无法在JSP页面渲染,以下是具体问题和修正方案:

1. JSP页面属性名不匹配+错误的属性获取方式

在PropertyAction中,你将查询到的列表存入了request的propertylist属性,但JSP代码存在两处关键错误:

  • 错误地从session而非request中获取属性
  • logic:iterate标签使用的name为prolist,与实际存储的属性名propertylist不匹配

修正后的view.jsp代码:

All Property: 
<br> 

<logic:iterate name="propertylist" id="property">
    <p>
        <bean:write name="property" property="id"/>,
        <bean:write name="property" property="pname"/>,
        <bean:write name="property" property="paddress"/>,
        <bean:write name="property" property="price"/>,
        <bean:write name="property" property="pcontact"/>
    </p>
</logic:iterate>

注:移除了多余的scriptlet代码,直接通过Struts标签从request读取属性

2. Hibernate Service层事务未正确处理

PropertyService的showAllproperty方法中,开启事务后未提交,也未关闭Session,这会导致Hibernate查询结果无法正确返回,同时造成数据库连接资源泄漏。

修正后的PropertyService代码:

public class PropertyService implements PropertyDao { // 补充实现接口
    @Override
    public List<PropertyAdd> showAllproperty() {
        List<PropertyAdd> propertylist = new ArrayList<>();
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("From PropertyAdd");
            propertylist = query.list();
            tx.commit(); // 提交事务
        } catch (HibernateException e) {
            if (tx != null) tx.rollback(); // 异常时回滚事务
            e.printStackTrace();
        } finally {
            if (session != null) session.close(); // 关闭Session释放资源
        }
        return propertylist;
    }
}

补充:让PropertyService实现PropertyDao接口,保证接口与实现的一致性

3. 其他需要验证的点

  • 检查struts-config.xml:确认PropertyAction的List方法对应的forward"show"是否正确指向view.jsp,同时确认ActionForm配置是否正确关联PropertyShow
  • 验证Hibernate配置:确认hibernate.cfg.xml已引入PropertyAdd的映射文件,且数据库连接配置正确
  • 调试数据:在PropertyAction中添加打印语句,输出propertylist.size(),确认Hibernate确实查询到了数据

内容的提问来源于stack exchange,提问作者suriya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 18:09:22