Struts2, SPRING4, HIBERNATE4 Integration Ultra-Detailed tutorial

Source: Internet
Author: User
Tags aop xmlns
Struts2, Spring4, HIBERNATE4 Integration Ultra-detailed tutorial

Struts2, Spring4, Hibernate4 consolidation Example-download

STRUTS2SPRING4HIBERNATE4 Integration Super-detailed tutorial join Spring join Hibernate join STRUTS2

Project Objective: To
build a prototype of the project architecture using the latest version of the three frameworks (i.e., Struts2, Spring4, and Hibernate4).
Project Architecture Prototype: Struts2 + spring4.0+ Hibernate4.2.4.
Project features: At the same time using Struts2, Spring4, Hibernate4, log4j and other libraries or frameworks, to build a most basic project prototype.
Join Spring

Add Spring required jar Package

Configure the Web. xml file

<?xml version= "1.0" encoding= "UTF-8"?> <web-app
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "
    xmlns=" Http://java.sun.com/xml/ns/javaee "
    xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
    id=" webapp_id "version=" 2.5 ">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath: applicationcontext*.xml</param-value>
    </context-param>

    <listener>
        < Listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
    </ Listener>


</web-app>
Join Spring's configuration file [Applicationcontext.xml]
<?xml version= "1.0" encoding= "UTF-8"?> <beans
xmlns= "Http://www.springframework.org/schema/beans"
    xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop= "http://www.springframework.org/ SCHEMA/AOP "
    xmlns:context=" Http://www.springframework.org/schema/context "
    xmlns:tx="/http/ Www.springframework.org/schema/tx "
    xsi:schemalocation=" Http://www.springframework.org/schema/beans/http Www.springframework.org/schema/beans/spring-beans.xsd
        HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/http Www.springframework.org/schema/aop/spring-aop-4.0.xsd
        Http://www.springframework.org/schema/context/http Www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http ://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">

</beans>
Join HibernateAdd the jar package required for hibernate

Join the Hibernate.cfg.xml file, where you configure the basic properties of Hibernate

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE hibernate-configuration public
        "-//hibernate/hibernate configuration DTD 3.0//en"
        "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
Integration with Spring

Join C3P0 and MySQL driver

New Db.properties

Jdbc.user=root
jdbc.password=1230
jdbc.driverclass=com.mysql.jdbc.driver
jdbc.jdbcurl=jdbc:mysql:/ /localhost:3306/test?useunicode=true&characterencoding=utf-8

jdbc.initpoolsize=5
jdbc.maxPoolSize= 10

Configure in Spring configuration file: Data source, Sessionfactory, declarative transaction

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/

    Spring-tx-4.0.xsd "> <context:annotation-config/> <context:component-scan base-package=" com "/> <!--import resource Files--<context:property-placeholder location= "ClasspaTh:db.properties "/> <!--configuration c3p0 data Source-<bean id=" DataSource "class=" com.mchange.v2.c3p0.ComboPooled DataSource "> <property name=" User "value=" ${jdbc.user} "></property> <property name=" Pass Word "value=" ${jdbc.password} "></property> <property name=" Driverclass "value=" ${jdbc.driverclass} " ;</property> <property name= "Jdbcurl" value= "${jdbc.jdbcurl}" ></property> <property Name= "Initialpoolsize" value= "${jdbc.initpoolsize}" ></property> <property name= "maxpoolsize" value= "${ Jdbc.maxpoolsize} "></property> </bean> <!--configuration Sessionfactory-<bean id=" Sessionfa Ctory "class=" Org.springframework.orm.hibernate4.LocalSessionFactoryBean "> <property name=" DataSource "ref=" DataSource "></property> <property name=" configlocation "value=" Classpath:hibernate.cfg.xml "></p Roperty> <pRoperty name= "mappinglocations" value= "Classpath:com/entities/*.hbm.xml" ></property> </bean> <! --Configure Spring's declarative transactions-<!--1. Configure Hibernate transaction Manager-<bean id= "TransactionManager" class= " Org.springframework.orm.hibernate4.HibernateTransactionManager "> <property name=" sessionfactory "ref=" Sessi Onfactory "></property> </bean> <!--2.
            Configure transaction Properties--<tx:advice id= "Txadvice" transaction-manager= "TransactionManager" > <tx:attributes> <tx:method name= "get*" read-only= "true"/> <tx:method name= "lastnameisvalid" read-only= "Tru E "/> <tx:method name=" * "/> </tx:attributes> </tx:advice> <!--3. Configure the transaction pointcut, and then associate the transaction properties with the transaction pointcut-<aop:config> <aop:pointcut expression= "Execution (* com.service.*.* (. .))" Id= "Txpointcut"/> <aop:advisor advice-ref= "Txadvice" pointcut-ref= "TxpointcUT "/> </aop:config> </beans>
 
Small test

New entity class Test.java

Package com.entities;

Import javax.persistence.Entity;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import javax.persistence.Table;

@Entity
@Table (name = "test") public
class Test {

    @Id
    @GeneratedValue (strategy = generationtype.identity)
    Private long id;//primary key
    private String name;


    Public long GetId () {
        return ID;
    }
    public void SetId (long id) {
        this.id = ID;
    }
    Public String GetName () {
        return name;
    }
    public void SetName (String name) {
        this.name = name;
    }

}

Hibernate.cfg.xml add

   <session-factory>
        <!--above ...-->
        <mapping class= "Com.entities.Test" ></mapping>
    </session-factory>

If successful, the test table is automatically generated in the database join Struts2

Add jar Package: If there are duplicate jar packages, you need to delete the lower version.

Configuring the Filter for Struts2 in the Web. xml file

    <!--configuration Struts2 Filter--
    <filter>
        <filter-name>struts2</filter-name>
        < Filter-class>org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        < Url-pattern>/*</url-pattern>
    </filter-mapping>
Add Struts2 config file, new Struts.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts public
    "-//apache software foundation//dtd struts Configuration 2.3//en"
    "/http Struts.apache.org/dtds/struts-2.3.dtd ">

<struts>

    <package name=" Default "namespace="/" extends= "Struts-default" >

            <action name= "Test" >
                <result>index.jsp</result>
            < /action>   

    </package>

</struts>
Test in Browser input http://localhost:8080/S2S4H4/test if there is no exception, the entire frame is built

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.