spring4.2.5+hibernate4.3.11+struts1.3.8 Integration Scheme II

Source: Internet
Author: User
Tags aop connection pooling xmlns ssh

We have integrated the spring4.2.5+hibernate4.3.11+struts1.3.8 in the front, but the integration is not perfect because we have to have this code every time we want to get the spring container instance in action:

Webapplicationcontext CTX = Webapplicationcontextutils.getwebapplicationcontext (
                this.getservlet (). Getservletcontext ());

is not very annoying ah. And it also leads to struts ' action being tightly coupled to spring, because we're using classes in spring. At this point we would like to be able to use spring-dependent injection into the way directly into it. We should know that if we are going to use spring's dependency injection, the bean must be handed over to the spring container for management. So, we're going to give the action to spring management so we can use dependency injection to inject the business layer's beans into the action. Note that you want to make sure that the Path property value of the action is the same as the bean name . If the <action> element in the struts configuration file is:

<action path= "/person/list" ...>

</action>

The following configuration should be added to the spring configuration file when the action is given to spring administration:

<bean name= "/person/list" class= "Cn.itcast.web.action.PersonAction"/>

In addition, we also add the request controller to spring in the struts configuration file, which first looks for the bean with the same name as the property value based on the value of the action's Path property to the spring container, and if found, uses the bean to process the user request. That is, add the following configuration to the struts configuration file:

<controller>
    <set-property property= "Processorclass"
        value= " Org.springframework.web.struts.DelegatingRequestProcessor "/>
</controller>

At this point, the struts configuration file--struts-config.xml is as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts-config public
          "-//apache software foundation//dtd struts Configuration 1.3//en"
          "/http Struts.apache.org/dtds/struts-config_1_3.dtd ">

<struts-config>
    <action-mappings>
        <action path= "/person/list" validate= "false" >
            <forward name= "list" path= "/web-inf/page/personlist.jsp "></forward>
        </action>
    </action-mappings>

    <controller>
        < Set-property property= "Processorclass"
            value= "Org.springframework.web.struts.DelegatingRequestProcessor"/ >
    </controller>

</struts-config>

The content of the spring configuration file--beans.xml is:

<?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:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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/context Http://www.springframework.org/schema/conte Xt/spring-context-4.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP Http://www.springframework.org/schema /aop/spring-aop-4.2.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx /spring-tx-4.2.xsd "> <context:annotation-config/> <bean id=" DataSource "class=" org.apache.commons.d Bcp. Basicdatasource "destroy-method=" Close ">
        <property name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/> <property name= "url" Value= "Jdbc:mysql://localhost:3306/jdbc?useunicode=true&amp;characterencoding=utf-8"/> <property nam
        E= "username" value= "root"/> <property name= "password" value= "Yezi"/> <!--initial value when connection pooling starts-- <property name= "InitialSize" value= "1"/> <!--connection Pool max--<property name= "Maxactiv E "value="/> <!--maximum idle value. After a peak time, the connection pool can slowly release a portion of the connection that has not been used, and has been reduced to maxidle-<property name= "Maxidle" value= "2"/> <! -Minimum idle value. When the number of free connections is less than the threshold, the connection pool will be pre-applied to some connections, so as not to apply the flood peak time-<property name= "Minidle" value= "1"/> </bean> & Lt;bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <pro Perty name= "DataSource" ref= "DataSource"/> <!--data Source--<property name= "Mappingresources "> <list> <value>cn/itcast/bean/Person.hbm.xml</value> &L t;! --Hibernate Entity Bean mapping file (can have multiple)-</list> </property> <!--Hibernatepropert
                IES is a property information that is used to configure Hibernate--<property name= "Hibernateproperties" > <value> Hibernate.dialect=org.hibernate.dialect.mysqldialect hibernate.hbm2ddl.auto=update Hiber Nate.show_sql=false hibernate.format_sql=false </value> </property> &L T;/bean> <!--Configure the transaction manager for Hibernate, the transaction manager manages the session created by the Sessionfactory object--<bean id= "Txmanager" CLA ss= "Org.springframework.orm.hibernate4.HibernateTransactionManager" > <property name= "sessionfactory" ref= "s Essionfactory "/> </bean> <tx:annotation-driven transaction-manager=" Txmanager "/> <bean id= "Personservice" class="Cn.itcast.service.impl.PersonServiceBean"/> <bean name= "/person/list" class= " Cn.itcast.web.action.PersonAction "/> </beans>

Note that we are going to import the following jar file into the SSH project:

Otherwise org.springframework.web.struts.DelegatingRequestProcessor the class will not be found, org.springframework.web.struts.DelegatingRequestProces Sor the process for this class is:

Thus, there are 47 jar files that need to be imported into the SSH project altogether:

As a result, the Personaction code should read:

public class Personaction extends Action {
    @Resource personservice personservice;

    @Override public
    Actionforward Execute (actionmapping mapping, actionform form, httpservletrequest request,
            HttpServletResponse response) throws Exception {
        Request.setattribute ("Persons", Personservice.getpersons ());
        return Mapping.findforward ("list");
    }

}

In this way, we can see that no classes are used in spring. Looking at the database person table, you can see that the person table has the following record:

At this point, we access the URL address through the browser: http://localhost:8080/SSH/person/list.do, you can see the following results:

At this point, the second scenario for spring4.2.5+hibernate4.3.11+struts1.3.8 integration is successful, and this approach is more elegant and should be done in real-world development. To view the source code, click spring4.2.5+hibernate4.3.11+struts1.3.8 Integration Scenario two to download.

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.