HIBERNATE+SPRING+STRUTS2 Integration

Source: Internet
Author: User

These three are all well-known frameworks for Java (SSH)
STRUTS2, as a controller, is the equivalent of a servlet that uses an interceptor mechanism to process a user's request.
Hibernate, persistence framework.
Spring,java Layered Lightweight framework. Used to manage, provide dependency injection, facets, etc.
If there is any mistake, please correct me.
SSH Framework Integration
In the first instance of the Web. XML configuration, one of the highlights of hibernate is lazy loading, which is the actual sending of SQL statements when you need them.
Add Opensessioninview is because if the first page needs to load data in the database when the delay load will cause the first page no data problems, as long as the use of the Hibernate framework, you should configure the Web page Opensessioninview.

<!--web. XML Configuration--
<!--Hibernateopensessioninview, to be managed by spring, this type of bit and spring ORM package, need to be downloaded separately, Opensessioninview needs to be configured on the Web. XML first if it is not possible to cause a problem. Versions are different, paths may be different--
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--struts Interceptor configuration-
<!--struts--
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--tomcat loads the Applicationcontext.xml file when it starts--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring Monitor--
<listener>
<listener-class>
Org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

ApplicationContext configuration (I use STS, automatically add namespaces, personal feel Convenient)
<!--The first step is to configure the DataSource, I am equipped with C3P0, the configuration of the big way are the same,-->
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<!--Drive-
<property name= "Driverclass" value= "Oracle.jdbc.driver.OracleDriver"/>
<!--connection address, username, password, hard-coded here, can also be imported--
<property name= "Jdbcurl" value= "Jdbc:oracle:thin: @localhost: 1521:orcl"/>
<property name= "User" value= "Scott"/>
<property name= "Password" value= "Scott"/>
<!--The maximum number of connections that are kept in the connection pool. Default value:---
<property name= "maxpoolsize" value= "/>"
<!--the minimum number of connections left in the connection pool, default is:3-->
<property name= "Minpoolsize" value= "2"/>
<!--the number of connections in the connection pool is initialized, the value should be between Minpoolsize and Maxpoolsize, and the default is 3-->
<property name= "Initialpoolsize" value= "2"/>
<!--maximum idle time, unused in 60 seconds, the connection is discarded. If 0, it will never be discarded. Default value: 0--
<property name= "MaxIdleTime" >60</property>
<!--when the connection pool connection is exhausted, the client calls getconnection () to wait for a new connection, and then throws SqlException after the timeout, and waits indefinitely if set to 0. Unit milliseconds. Default: 0--
<property name= "checkouttimeout" value= "/>"
<!--c3p0 The number of connections that are fetched at the same time when the connection in the connection pool is exhausted. Default value: 3--
<property name= "Acquireincrement" value= "2"/>
<!--defines the number of repeated attempts to obtain a new connection from the database after a failure. Default value: 30, less than or equal to 0 means infinite--
<property name= "acquireretryattempts" value= "0"/>
<!--retry interval, by default: 1000 milliseconds--
<property name= "Acquireretrydelay" value= "/>"
<!--If the uncommitted transaction is committed when the connection is closed, the default is False, which is to close the connection and roll back the uncommitted transaction--
<property name= "Autocommitonclose" >false</property>
<!--C3P0 will build a blank table named Test and test it with its own query statement. If this parameter is defined, then the attribute preferredtestquery will be ignored. You cannot do anything on this test sheet, it will be used only for C3P0 testing. Default value: null--
<property name= "Automatictesttable" >Test</property>
<!--If False, getting a connection failure will cause all threads that wait for the connection pool to get the connection to throw an exception, but the data source is still valid and continues to try to get the connection the next time it calls Getconnection (). If set to True, the data source will declare broken and permanently shut down after attempting to acquire a connection failure. Default: False-->
<property name= "Breakafteracquirefailure" >false</property>
<!--check for idle connections in all connection pools every 60 seconds. Default value: 0, not checked--
<property name= "Idleconnectiontestperiod" >60</property>
<!--c3p0 the size of the global preparedstatements cache. If both maxstatements and maxstatementsperconnection are 0, the cache does not take effect, and if one is not 0, the statement's cache will take effect. If the default value: 0-->
<property name= "Maxstatements" >100</property>
<!--maxstatementsperconnection defines the maximum number of cache statements that a single connection in a connection pool has. Default value: 0--
<property name= "Maxstatementsperconnection" ></property>
</bean>
<!-I'm using the entity class to generate the table, not in fact, but very similar, I will write a note--
<!--Sessionfactory, the session used to generate the Session,dao (persistence layer) is taken from here.
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<!-- Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean This class has a property called DataSource, which needs to refer to datasource (the c3p0 configured above) data Source--
<property name= "DataSource" ref= "DataSource" ></property>
<!--All-inclusive scan, generate all the entity classes in the entity package, note that if there is a database keyword or entity class with a duplicate, the table may fail to generate (the repetition will fail)-
<property name= "Packagestoscan" >
<list>
<value>entity</value>
</list>
</property>
<!--Hibernate property Configuration--
<property name= "Hibernateproperties" >
<props>
<!--the first time the table is generated, update should be changed to create, then update, or each table will be regenerated--
<prop key= "Hibernate.hbm2ddl.auto" >update</prop>
<prop key= "Hibernate.format_sql" >true</prop>
<prop key= "Hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</prop>
<prop key= "Hibernate.show_sql" >true</prop>
<!--<prop key= "Hibernate.current_session_context_class" >thread</prop>-
</props>
</property>
</bean>

<!--declarative transactions--
<bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>
<!--management of transactions, Hibernate has five kinds of transaction mechanism, here does not explain, read-only= "true" type read-only transaction, can read only, cannot modify, name= "login*" here also uses the wildcard character--
<tx:advice id= "Txadvice" transaction-manager= "Txmanager" >
<tx:attributes>
<tx:method name= "login*" propagation= "REQUIRED" rollback-for= "Java.lang.Exception" read-only= "true"/>
<tx:method name= "query*" propagation= "REQUIRED" rollback-for= "Java.lang.Exception" read-only= "true"/>
<tx:method name= "add*" propagation= "REQUIRED" rollback-for= "Java.lang.Exception"/>
<tx:method name= "get*" propagation= "REQUIRED" rollback-for= "Java.lang.Exception"/>
<tx:method name= "update*" propagation= "REQUIRED" rollback-for= "Java.lang.Exception"/>
<tx:method name= "modify*" propagation= "REQUIRED" rollback-for= "Java.lang.Exception"/>
<tx:method name= "del" rollback-for= "Java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<!--transaction enhancement via AOP, primarily for the DAO layer, expression= "Execution (* dao.*.* (..))" This uses a wildcard character--
<aop:config>
<aop:pointcut id= "Alltestservicemethod" expression= "Execution (* dao.*.* (..))" />
<aop:advisor pointcut-ref= "Alltestservicemethod" advice-ref= "Txadvice"/>
</aop:config>

<!--struts2.xml Configuration--
<!--is managed by spring, and the high-struts2 default is to spring management, which opens the Struts profile view-
<constant name= "struts.objectfactory" value= "Spring"/>
<!--There are two ways to do this action--
<!--first, the class of action does not have to write the fully qualified name, but the bean inside the Applicationcontext.xml is the name of the class here, and the class for the bean needs to write out the fully qualified name--
<action name= "loginaction" class= "Loginaction" >
<result name= "Success" >loginOk.jsp</result>
<result name= "ERROR" >index.jsp</result>
</action>

<!--the second type, the action corresponding class writes out the fully qualified name, but the bean inside the Applicationcontext.xml is the name of the action that references Struts.xml--
<action name= "loginaction" class= "Loginaction" >
<result name= "Success" >loginOk.jsp</result>
<result name= "ERROR" >err.jsp</result>
</action>
<!--Dao,biz,service,impl are all written in beans--


The above is SSH integration. SSH integration requires downloading the appropriate jar package.
If there is any mistake, please correct me.

HIBERNATE+SPRING+STRUTS2 Integration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.