如何将 Struts 2 与 Hibernate 和 PostgreSQL 连接起来 [英] How to connect Struts 2 with Hibernate and PostgreSQL

查看:29
本文介绍了如何将 Struts 2 与 Hibernate 和 PostgreSQL 连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将Struts 2 与hibernate 和PostgreSQL 连接起来?

How to connect Struts 2 with hibernate and PostgreSQL?

<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/jvmhubtutorial</property>
<property name="hibernate.connection.username">user</property>
<property name="hibernate.connection.password">password</property>

推荐答案

您可以通过 servlet 上下文集成 Hibernate 和 Struts2,您可以共享会话工厂.会话工厂用于打开 Hibernate 会话并使用它来执行对数据库的查询.这里是这种集成的示例.

You can integrate Hibernate and Struts2 via servlet context where you can share the session factory. The session factory is used to open Hibernate session and use it to perform queries to the database. Here an example of such integration.

在 Struts2 中,没有官方插件可以集成 Hibernate框架.但是,您可以通过以下步骤解决:

In Struts2, there are no official plugins to integrate the Hibernate framework. But, you can workaround with the following steps :

  1. 注册一个自定义的ServletContextListener.
  2. ServletContextListener 类中,初始化 Hibernate 会话并将其存储到 servlet 上下文中.
  3. 在动作类中,从 servlet 上下文中获取 Hibernate 会话,并照常执行 Hibernate 任务.
  1. Register a custom ServletContextListener.
  2. In the ServletContextListener class, initialize the Hibernate session and store it into the servlet context.
  3. In action class, get the Hibernate session from the servlet context, and perform the Hibernate task as normal.

在 Struts2 中有一个名为 Struts2 Full Hibernate Plugin 的非官方插件或 提供与 Hibernate 的集成.有例子:

In Struts2 there's unofficial plugin called Struts2 Full Hibernate Plugin or struts2-s2hibernate that provides an integration with Hibernate. There're examples:

这篇关于如何将 Struts 2 与 Hibernate 和 PostgreSQL 连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆