<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    zhyiwww
    用平實的筆,記錄編程路上的點點滴滴………
    posts - 536,comments - 394,trackbacks - 0

    HibernatePlugIn for Struts

    To configure Hibernate in a Jakarta-Struts application:

    1. Create your struts-config.xml file.

    2. Add the following configuration for the plugin:

    <plug-in className="edu.arbor.util.plugin.HibernatePlugIn">

    ? <!-- 'path-to-config-file' is relative to the root of the class

    ?????? path.? It MUST start with a '/'. The default is

    ?????? "/hibernate.cfg.xml" -->

    ? <set-property property="configFilePath" value="path-to-config-file" />

    ? <set-property property="storeInServletContext" value="true-or-false" />

    </plug-in>

    ?

    3. Copy the code into a source file and compile.

    package edu.arbor.util.plugin;

    ?

    import java.net.URL;

    import javax.servlet.ServletContext;

    import javax.servlet.ServletException;

    import net.sf.hibernate.SessionFactory;

    import net.sf.hibernate.cfg.Configuration;

    import org.apache.commons.logging.Log;

    import org.apache.commons.logging.LogFactory;

    import org.apache.struts.action.ActionServlet;

    import org.apache.struts.action.PlugIn;

    import org.apache.struts.config.ModuleConfig;

    ?

    /**

    ?* Implements the <code>PlugIn</code> interface to configure the Hibernate

    ?* data persistence library.? A configured

    ?* <code>net.sf.hibernate.SessionFactory</code> is stored in the

    ?* <code>ServletContext</code> of the web application unless the property

    ?* <code>storedInServletContext</code> is set to <code>false</code>.

    ?*

    ?* <plugin class="net.sf.hibernate.plugins.struts.HibernatePlugIn">

    ?*?? <set-property name="configFilePath"

    ?*?????????????? ??value="path-to-config-file"/>

    ?*?? <set-property name="storedInServletContext&quot"

    ?*??????????????? value="true-or-false"/>

    ?* </plugin>

    ?*

    ?* @author? <a href="mailto:bhandy@users.sf.net">Bradley M. Handy</a>

    ?* @version 1.0

    ?*/

    public class HibernatePlugIn implements PlugIn {

    ???

    ??? /**

    ???? * the key under which the <code>SessionFactory</code> instance is stored

    ???? * in the <code>ServletContext</code>.

    ???? */

    ??? public static final String SESSION_FACTORY_KEY

    ??????????? = SessionFactory.class.getName();

    ?

    ??? private static Log _log = LogFactory.getLog(HibernatePlugIn.class);

    ???

    ??? /**

    ???? * indicates whether the <code>SessionFactory</code> instance will be stored

    ???? * in the <code>ServletContext</code>, or not.

    ???? */

    ??? private boolean _storedInServletContext = true;

    ???

    ??? /**

    ???? * the path to the xml configuration file.? the path should start with a

    ???? * '/' character and be relative to the root of the class path.

    ???? * (DEFAULT:? "/hibernate.cfg.xml")

    ???? */

    ??? private String _configFilePath = "/hibernate.cfg.xml";

    ?

    ??? private ActionServlet _servlet = null;

    ??? private ModuleConfig _config = null;

    ??? private SessionFactory _factory = null;

    ?

    ??? /**

    ???? * Destroys the <code>SessionFactory</code> instance.

    ???? */

    ??? public void destroy() {

    ??????? _servlet = null;

    ??????? _config = null;

    ???????

    ??????? try {

    ??????????? _log.debug("Destroying SessionFactory...");

    ???????????

    ??????????? _factory.close();

    ???????????

    ??????????? _log.debug("SessionFactory destroyed...");

    ??????? } catch (Exception e) {

    ??????????? _log.error("Unable to destroy SessionFactory...(exception ignored)",

    ??????????????????? e);

    ??????? }

    ??? }

    ???

    ??? /**

    ???? * Initializes the <code>SessionFactory</code>.

    ???? * @param servlet the <code>ActionServlet</code> instance under which the

    ???? *??????? plugin will run.

    ???? * @param config the <code>ModuleConfig</code> for the module under which

    ???? *??????? the plugin will run.

    ???? */

    ??? public void init(ActionServlet servlet, ModuleConfig config)

    ??? throws ServletException {

    ??????? _servlet = servlet;

    ??????? _config = config;

    ???????

    ??????? initHibernate();

    ??? }

    ???

    ??? /**

    ???? * Initializes Hibernate with the config file found at

    ???? * <code>configFilePath</code>.

    ???? */

    ??? private void initHibernate() throws ServletException {

    ??????? Configuration configuration = null;

    ??????? URL configFileURL = null;

    ??????? ServletContext context = null;

    ???????

    ??????? try {

    ??????????? configFileURL = HibernatePlugIn.class.getResource(_configFilePath);

    ?

    ??????????? context = _servlet.getServletContext();

    ?

    ??????????? if (_log.isDebugEnabled()) {

    ??????????????? _log.debug("Initializing Hibernate from "

    ??????????????????????? + _configFilePath + "...");

    ??????????? }

    ???????????

    ??????????? configuration = (new Configuration()).configure(configFileURL);

    ??????????? _factory = configuration.buildSessionFactory();

    ???????????

    ??????????? if (_storedInServletContext) {

    ??????????????? _log.debug("Storing SessionFactory in ServletContext...");

    ??? ????????????

    ??????????????? context.setAttribute(SESSION_FACTORY_KEY, _factory);

    ??????????? }

    ??????????

    ??????? } catch (Throwable t) {

    ??????????? _log.error("Exception while initializing Hibernate.");

    ??????????? _log.error("Rethrowing exception...", t);

    ???????????

    ??????????? throw (new ServletException(t));

    ??????? }

    ??? }

    ???

    ??? /**

    ???? * Setter for property configFilePath.

    ???? * @param configFilePath New value of property configFilePath.

    ???? */

    ??? public void setConfigFilePath(String configFilePath) {

    ??????? if ((configFilePath == null) || (configFilePath.trim().length() == 0)) {

    ??????????? throw new IllegalArgumentException(

    ??????????????????? "configFilePath cannot be blank or null.");

    ??????? }

    ???????

    ??????? if (_log.isDebugEnabled()) {

    ??????????? _log.debug("Setting 'configFilePath' to '"

    ??????????????????? + configFilePath + "'...");

    ??????? }

    ???????

    ??????? _configFilePath = configFilePath;

    ??? }

    ???

    ??? /**

    ???? * Setter for property storedInServletContext.

    ???? * @param storedInServletContext New value of property storedInServletContext.

    ???? */

    ??? public void setStoredInServletContext(String storedInServletContext) {

    ??????? if ((storedInServletContext == null)

    ??????????????? || (storedInServletContext.trim().length() == 0)) {

    ??????????? storedInServletContext = "false";

    ??????? }

    ???????

    ??????? if (_log.isDebugEnabled()) {

    ??????????? _log.debug("Setting 'storedInServletContext' to '"

    ??????????????????? + storedInServletContext + "'...");

    ??????? }

    ???????

    ??????? _storedInServletContext

    ??????????????? = new Boolean(storedInServletContext).booleanValue();

    ??? }

    ???

    }

    ?

    ?

    A Usage example would be excellent!

    08 Sep 2003, 16:07

    themax

    A usage example would be excellent!

    ?

    Some more meat in the bones. Configruing hibernate with struts

    26 Nov 2003, 14:12

    mark.lowe

    <?xml version='1.0' encoding='utf-8'?>

    <!DOCTYPE hibernate-configuration PUBLIC

    ??????? "-//Hibernate/Hibernate Configuration DTD 2.0//EN"

    ????????????? "http://hibernate.sourceforge.net/hibernate-configuration

    -2.0.dtd">

    ?

    <hibernate-configuration>

    ?

    ??? <!-- a SessionFactory instance listed as /jndi/name -->

    ?? <session-factory name="foo:/hibernate/SessionFactory">

    ??????? <!-- properties -->

    ??????? <property

    name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

    ??????? <property name="connection.username">brian</property>

    ??????? <property name="connection.password">arses</property>

    ??????? <property name="connection.url">jdbc:mysql://localhost:3306/

    mydata</property>

    ??????? <property

    name="connection.driver_class">com.mysql.jdbc.Driver</property>

    ??????? <property name="show_sql">true</property>

    ??????? <property name="use_outer_join">true</property>

    ??????? <property

    name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTra

    nsactionFactory</property>

    ??????? <property name="dbcp.minIdle">1</property>

    ??????? <property name="cache.use_query_cache">true</property>

    ?????????????

    ??????? <!-- mapping files -->

    ??????? <mapping resource="com/sparrow/om/Arse.hbm.xml"/>

    ?

    ??? </session-factory>

    ?

    </hibernate-configuration>

    ?

    ?

    and in struts config..

    ?

    ?????? <plug-in className="edu.arbor.util.plugin.HibernatePlugIn">

    ????????????? <set-property property="storedInServletContext" value="true"/>

    ?????? </plug-in>

    ?

    ..

    in struts action

    ..

    Context ctx = new InitialContext();

    SessionFactory sf = (SessionFactory) ctx.lookup("foo:/hibernate/

    SessionFactory");

    hibSession = sf.openSession();

    ?

    Query query = hibSession.createQuery("from com.sparrow.om.Arse");

    Iterator it = query.iterate();

    ????????????????????

    while(it.hasNext()) {

    ????????????? Arse arse = (Arse) it.next();

    ????????????? arseList.add(arse);

    }

    ????????????????????

    request.setAttribute("arses", arseList.toArray());

    ?

    ..

    ?

    Tomcat notes

    23 Aug 2004, 18:03

    chrismaeda

    The previous example (where the SessionFactory is bound in the JNDI

    tree) doesn't work for Tomcat since Tomcat's JNDI is read-only.

    ?

    If you store the SessionFactory in the servlet context, you get it

    back with the following code:

    ?

    ?? (SessionFactory) servletContext.getAttribute

    (HibernatePlugIn.SESSION_FACTORY_KEY);

    ?

    If you are using JSP 1.2 or 2.0, you could also use this EL expression:

    ?

    ?? "${applicationScope['net.sf.hibernate.SessionFactory']}"

    ?

    This works for generic servlets as well.? Just execute the plugin code

    in the servlet's init() method.

    ?



    |----------------------------------------------------------------------------------------|
                               版權聲明  版權所有 @zhyiwww
                引用請注明來源 http://m.tkk7.com/zhyiwww   
    |----------------------------------------------------------------------------------------|
    posted on 2006-04-20 14:57 zhyiwww 閱讀(279) 評論(0)  編輯  收藏 所屬分類: j2ee
    主站蜘蛛池模板: 亚洲愉拍99热成人精品热久久| 四虎影院永久免费观看| 无码欧精品亚洲日韩一区| 国产国产人免费人成成免视频| 亚洲国产精品专区在线观看| 免费很黄无遮挡的视频毛片| 亚洲国产精品日韩| www免费黄色网| 久久亚洲国产午夜精品理论片| 免费无码中文字幕A级毛片| 亚洲视频手机在线| 日本精品人妻无码免费大全| 亚洲综合国产成人丁香五月激情| 毛片免费在线观看网站| 99亚洲乱人伦aⅴ精品| 亚洲国产精品自产在线播放| av永久免费网站在线观看| 久久久无码精品亚洲日韩按摩 | 无人影院手机版在线观看免费| 亚洲丰满熟女一区二区v| 成人免费福利电影| 美女视频黄a视频全免费网站色| 亚洲中久无码不卡永久在线观看| 国产成人免费AV在线播放 | 亚洲精品无播放器在线播放| 国产在线观看免费视频播放器| 国产免费内射又粗又爽密桃视频| 67pao强力打造67194在线午夜亚洲| 国产无人区码卡二卡三卡免费| 亚洲GV天堂GV无码男同| 国产亚洲精品精品国产亚洲综合 | 久久免费视频观看| 国产91在线|亚洲| 国产精品亚洲综合专区片高清久久久| 美女视频黄的免费视频网页| 国产成人精品日本亚洲18图| 亚洲欧洲中文日韩久久AV乱码| 中文字幕在线免费| 免费无码又爽又黄又刺激网站| 99ri精品国产亚洲| 亚洲国产成人精品91久久久|