Java EE SSH Framework Integration Tutorial

Source: Internet
Author: User

This article only as a reference for study and research, and the actual project use of technology is different, because the author level is limited, error omission unavoidable, please crossing criticism advice.

The project's source code is placed in: Https://github.com/Frank-Pei/SSHIntegrationDemo

Overview of using the SOFTWARE environment: jdk1.7.0_79, Eclipse Mars4.5, oracle11gr2 version 11.2.0.3, Tomcat7.0.65, Maven2

The SSH framework actually refers to the integration of spring, Struts, Hibernate, and SSH frameworks as well as the integration of Spring+struts and spring+hibernate.

The integration steps are as follows:

(1) Add three framework jar files and create related configuration files.

(2) Add a data source for hibernate.

(3) Configuring Sessionfactory objects

(4) Implement and configure DAO and service.

(5) Use spring to simplify database transaction processing, that is, to configure declarative transaction management.

(6) Implement and configure the action.

Where (2)-(5) belongs to Spring integration Hibernate, step (6) belongs to spring integrated Struts2.

(a) Construction of the engineering environment

1. Starting with the MAVEN project, archetype Select WebApp

2. Group ID & Artifact ID

3. Open the project properties after creation, modify the JRE in Build path (the default MAVEN project will use JRE1.5)

4. When you click Remove, the ADD library selects the default Jre7

5. The creation of the completed MAVEN project will result in an error because the Tomcat is not added to the build path.

6. The basic work has been completed. Start adding the jar files needed for the project.

(ii) Integration of SPRING2 and Hibernate3

1. Configure the data source in the Web container, the data source uses the connection pool technology (Connection pool), the common connection pool technology has C3P0, DBCP, Proxool, here we use C3P0

First, you need to c3p0 the jar package, you can refer to the Pom.xml file at the end of this article, and you need to put the Oracle database jar file under the Tomcat installation directory under the lib/subdirectory.

Second, the configuration will have to modify three files, namely the Tomcat installation directory context.xml, web.xml,spring configuration file. The code is as follows:

Configuration in the Context.xml,

<Resourcename= "JDBC/ORCL"Auth= "Container"type= "Javax.sql.DataSource"Driverclassname= "Oracle.jdbc.OracleDriver"URL= "JDBC:ORACLE:THIN:@192.168.0.10:1521:ORCL"username= "Frank"Password= "Frank"maxactive= " the"Maxidle= "Ten"maxwait= "-1"/>

Configuration in Web. XML,

<Resource-ref>     <Description>Oracle Datasource Example</Description>     <Res-ref-name>Jdbc/orcl</Res-ref-name>     <Res-type>Javax.sql.DataSource</Res-type>     <Res-auth>Container</Res-auth></Resource-ref>

The configuration in the spring configuration file,

<id= "DataSource"  class= " Org.springframework.jndi.JndiObjectFactoryBean ">    <name = "Jndiname" value = "JAVA:COMP/ENV/JDBC/ORCL" ></  Property > </ Bean >

2. Configure Sessionfactory, you must add Spring-orm and Aspectjweaver jar packages

<BeanID= "Sessionfactory"class= "Org.springframework.orm.hibernate3.LocalSessionFactoryBean">        <!--DataSource is used to develop a data source, ref references the Bean with ID datasource defined above -        < Propertyname= "DataSource"ref= "DataSource" />        <!--Specify the location of the seesionfactory to find the configuration file another approach is as follows, if you want to create multiple profiles you must manually add <property name= "Mappingresources" > <list> <value>com/example/domain/Student.hbm.xml</value> </list> </property> -        < Propertyname= "Mappinglocations">            <List>                <value>Classpath:**/*.hbm.xml</value>            </List>        </ Property>        <!--Configure attributes such as dialects -        < Propertyname= "Hibernateproperties">            <Props>                <propKey= "Hibernate.dialect">Org.hibernate.dialect.Oracle10gDialect</prop>                <propKey= "Hibernate.show_sql">True</prop>                <propKey= "Hibernate.format_sql">True</prop>            </Props>        </ Property>    </Bean>

3. Configure DAO and service, only configure the DAO layer for simplification, no write service layer

Configure the DAO in spring as follows:

<id= "Studentdao"  class= "Com.example.dao.StudentDaoImpl " >        <  name= "Sessionfactory"  ref= "sessionfactory"/>    </bean>

4. Configure declarative transactions (declarative Transaction), based on the spring AOP feature

<!--transaction Manager, Spring provides the transaction management class for Hibernate Hibernatetransactionmanager -    <BeanID= "TransactionManager"class= "Org.springframework.orm.hibernate3.HibernateTransactionManager">        < Propertyname= "Sessionfactory"ref= "Sessionfactory" />    </Bean>    <!--defining an AOP pointcut for transactions -    <Aop:config>        <Aop:pointcutID= "Productservicemethods"expression= "Execution (* com.example.dao.*.* (..))" />        <Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Productservicemethods" />    </Aop:config>    <!--Configure transaction Properties -    <Tx:adviceID= "Txadvice"Transaction-manager= "TransactionManager">        <tx:attributes>            <Tx:methodname= "save*"Propagation= "REQUIRED" />            <Tx:methodname="*"Propagation= "SUPPORTS"read-only= "true" />        </tx:attributes>    </Tx:advice>

At this point, the integration of spring and hibernate is complete.

(iii) Integration of SPRING2 and STRUTS2

In addition to Spring-context, the Struts-core jar package also requires a Struts-spring-plugin package

1. Configure the Listener in Web. XML and provide the location parameters of the spring configuration file

<!--Contextloaderlistener can initialize the spring container at Tomcat boot to complete the spring load -    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>    <!--Contextconfiglocation provides the path to read the spring configuration file -    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:applicationcontext-*.xml</Param-value>    </Context-param>

2. Configure the action in the spring container

The action is configured in spring as follows:

<id= "Studentaction"  class= "Com.example.web.action.StudentAction ">        <name=" Studentdao "  ref=" Studentdao "></Property>    </Bean >

The action in Struts.xml is configured as follows:

<!---        <  name = "Add" class = "Studentaction"  method= "Add">            <result >/hello.jsp</result>        </Action  >

At this point, the integration process of the SSH framework is complete.

Java EE SSH Framework Integration Tutorial

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.