Announcement Announcement Module
Collapse
No announcement yet.
struts2 + spring + hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • struts2 + spring + hibernate

    hello,
    i'm a newbie in these 3 technologies.
    i'm developing a web application using these. its working successfully. problem is, i've used transaction but not clear whether transaction is happening or not.

    here is my part of the applicationContext.xml file where i've added transaction. would u plz tell me i'm right or wrong.
    <bean id="UserDaoTarget" class="com.vonair.VonairNexus.dao.hibernate.UserHi bernateDaoImpl">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>

    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory"/>
    </property>
    </bean>

    <bean id="UserDao" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="target">
    <ref bean="UserDaoTarget"/>
    </property>
    <property name="transactionAttributes">
    <props>

    <prop key="addUser">PROPAGATION_REQUIRED</prop>
    <prop key="modifyUser">PROPAGATION_REQUIRED</prop>
    <prop key="deleteUser">PROPAGATION_REQUIRED</prop>
    <prop key="findById">PROPAGATION_REQUIRED,read-only</prop>
    </props>
    </property>
    </bean>

    <bean id="userAction"
    class="com.vonair.VonairNexus.action.UserAction" >
    <constructor-arg> <ref bean="UserDaoTarget"/></constructor-arg>
    </bean>
    here userAction is my action class. my confusion is
    <bean id="userAction"
    class="com.vonair.VonairNexus.action.UserAction" >
    <constructor-arg>
    <ref bean="UserDaoTarget"/>
    </constructor-arg>
    </bean>
    should i use UserDaoTarget or UserDao to make the transaction happen?

  • #2
    You should use the UserDAO.

    The UserDAOTarget is your "original" DAO, whilst the UserDAO is a "spring-enhanced" version of this bean, which is able to handle transactions.

    Comment


    • #3
      thank u.
      but now how can i call UserDaoTarget's method (which would do actual CRUD function) from my action class?

      Comment


      • #4
        UserDAOTarget is a "spring-enhanced" version of UserDAO : it still has the same methods, but those methods now "understand" transactions.
        You can call its methods directly, it's a UserDAO object.

        Comment


        • #5
          correct me plz.
          let target is a UserDaoTarget type instance.
          if i call target.update() method from my action, then update will happen within a transaction ?

          Comment


          • #6
            correct me plz.
            let target is a UserDaoTarget type instance.
            if i call target.update() method from my action, then update will happen within a transaction ?

            Comment


            • #7
              UserDaoTarget is an instance of UserHibernateDaoImpl (that's the first bean you declare). I guess this UserHibernateDaoImpl class implements a UserDao interface.

              Concerning the UserDao bean, you inject it into your Action constructor (I note the use of constructor-based injection in your Action). This bean is an enhanced version of UserDaoTarget, so it implements the same UserDao interface.

              So you can use this UserDao interface that is injected into your constructor directly. I guess it something like userDao.update(..) should work.

              And this bean will indeed "understand" transactions. The best way to be sure would be that you test it, why not make the transaction fail and see that it rolls back correctly?

              Comment


              • #8
                thanks a lot julien.

                Comment


                • #9
                  To avoid the problem of looking up or injecting the wrong bean, lots of people use inner beans.
                  http://www.springframework.org/docs/...ns-inner-beans
                  Last edited by karldmoore; Aug 29th, 2007, 10:29 AM.

                  Comment


                  • #10
                    Hi Sheetal,

                    Can you help me to configure the struts2 + spring ? May you please send the configuration steps that we have to follow... Are u using maven also?

                    thanking you
                    Lijin

                    Comment


                    • #11
                      And sheetal in the case of transactions we used a facade. I think may be this will be a good solution for this.

                      Comment


                      • #12
                        hello Lijin,
                        actually i'm still learning these. so there's very littile that i could help.
                        steps u have to follow are :
                        1. write the web.xml file like this:
                        <?xml version="1.0" encoding="UTF-8"?>
                        <web-app id="person" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
                        <display-name>person</display-name>
                        <filter>
                        <filter-name>struts2</filter-name>
                        <filter-class>
                        org.apache.struts2.dispatcher.FilterDispatcher
                        </filter-class>


                        </filter>
                        <filter-mapping>
                        <filter-name>struts2</filter-name>
                        <url-pattern>/*</url-pattern>
                        </filter-mapping>


                        <welcome-file-list>
                        <welcome-file>/login.jsp</welcome-file>
                        </welcome-file-list>
                        <!-- this step is to add spring with struts2 -->
                        <listener>
                        <listener-class>
                        org.springframework.web.context.ContextLoaderListe ner
                        </listener-class>
                        </listener>


                        </web-app>
                        2. write applicationContext.xml. it should empty for now. place it in the same folder as ur web.xml
                        <?xml version="1.0" encoding="UTF-8"?>
                        <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
                        "http://www.springframework.org/dtd/spring-beans.dtd">

                        <!--
                        - Application context definition
                        -->
                        <beans>

                        </beans>
                        if u've used struts2 others things are same as before. jars u r gonna need are :
                        1.spring.jar
                        2.struts2-spring-plugin.jar

                        Comment


                        • #13
                          Thank you very much Sheetal for your help. Thanks for giving the config help.

                          In our project we are working on Spring currently. We want to integrate Struts2 to spring. But when i tried to configure struts 2.. its not working for me. I think struts can directly access / read application context beans. But my struts2 configuration is not working..

                          Will post more outcomes in the future here.

                          Thanks for your support friend..
                          Lijin

                          Comment


                          • #14
                            can u post the errors u r getting? is that the dreadful error filterStart?

                            Comment


                            • #15
                              Hi Sheetal,

                              Actually I had configured struts with Zero configuration like specifing the struts action namespace in the web.xml. At that time it worked.

                              But when i declare the actions in struts.xml it is not redirecting to the corresponding jsp page. No error is coming but just dont getting that action in browser

                              Thanking you
                              Lijin

                              Comment

                              Working...
                              X