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

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

问题描述

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

hibernate.connection.driver_class> org.postgresql.Driver< /性>
< property name =hibernate.connection.url> jdbc:postgresql:// localhost / jvmhubtutorial< / property>
< property name =hibernate.connection.username>用户< / property>
< property name =hibernate.connection.password>密码< / property>


解决方案

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


在Struts2中,没有官方插件来集成Hibernate
框架。但是,您可以通过以下步骤解决:注册一个自定义 ServletContextListener



  1. ServletContextListener 类中,初始化Hibernate会话并将其存储到servlet上下文中。

  2. 在action类中,从servlet上下文获取Hibernate会话,并像平常一样执行Hibernate任务。



$ b $在Struts2中有非官方的插件叫做Struts2 Full Hibernate Plugin或者 struts2-s2hibernate ,它提供了与Hibernate的集成。有例子:


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>

解决方案

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.

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

  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.

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

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

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