File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts2 and Spring integration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 and Spring integration " Watch "Struts2 and Spring integration " New topic
Author

Struts2 and Spring integration

Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Hi All

I've checked many tutorial for this but i'm not able to get the exact idea how to integrate Spring and Struts2. Please let me know if anybody has come across any good tutorial which will explain step-by-step right from downloading libraries up to integration.

Thanks
Nain
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Check this http://www.coderanch.com/t/472077/Struts/Struts-Tomcat


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

The link however does not mention any step by step guide to integrating Spring and Struts, but will give you an idea on the integration aspect.
Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Hi

Thanks Jothi. But it's really difficult to understand from that piece of information. Could you please suggest any tutorial from where you have developed such application.

Thanks,
Navin
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

It is one application that I'm developing for a community. It is built using Maven / Spring / Struts / JPA. Can you let me know what is your project's constituents so that I can help you furthur.? But as a basic information, you need to have a struts-spring plugin jar and add it to your lib folder of your web app.
Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Thanks Jothi for such a prompt reply.

Our client wants us to develop application using Struts2 and Spring(AOP, DAO). The integration that works fine for Struts1.3.X is not working for Struts2. We just add a tag in struts-config.xml in case of Struts1.3.X but same is not working for Struts2.

It will be very much helpful if you tell me which configuration files we need and what tags should be used for the same to integrate Struts2 and Spring

Thanks
Navin
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Search for the following jar file in the internet and add that to your lib folder of the web application

struts2-spring-plugin

Next in your web.xml, add the following entry:



For the Spring integration of your web application, add the following again to the web.xml. Hope you have your applicationContext.xml in place:



Also register the Spring listeners in your web.xml:

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Consult the Spring reference documentation for a broader understanding.
Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Hi Jothi

It's really a great help. Now i'm facing another problem. I'm getting following exception.



In struts.xml i've mapped action like


And in applicationContext.xml


In Action class we don't have any arguments for execute() method like that i've


So do i need to change "org.springframework.web.struts.DelegatingActionProxy" to something else so that it will call execute method from our Action class.

Thanks,
Navin
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

What is that DelegationActionProxy?? Write your action class and map that in your struts.xml file.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Better spend some time reading the action mapping definitions of Struts 2.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It looks like you're trying to use Spring's Struts 1 support. Don't do that.
Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Hi Jothi

If we map Action class in struts.xml directly then how can we say that we are integrating it with Spring. In this case what's the use of applicationContext.xml

In Struts1 we do like this only. "org.springframework.web.struts.DelegatingActionProxy" create class object using reflection. So struts.xml is used to define the path and applicationContext.xml is used to map the action class to specified path just like mentioned below.

struts-config.xml


applicationContext.xml


Could you elaborate more on this please? I've got confused now.

Thanks
Navin
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Navin Pillu wrote:Hi Jothi

If we map Action class in struts.xml directly then how can we say that we are integrating it with Spring. In this case what's the use of applicationContext.xml

In Struts1 we do like this only. "org.springframework.web.struts.DelegatingActionProxy" create class object using reflection. So struts.xml is used to define the path and applicationContext.xml is used to map the action class to specified path just like mentioned below.

struts-config.xml


applicationContext.xml


Could you elaborate more on this please? I've got confused now.

Thanks
Navin


Spring is a DI container. By integrating Spring in your Struts application, it means that you inject your action classes using Spring's DI mechanism.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Get rid of that DelegationContext!
Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
If we map action path to Action class in struts.xml, then we are not using DI of spring and applicationContext.xml is of no use. Can you paste dummy code from your application for struts.xml and applicationContext.xml to understand it properly.

I want to use AOP on execute() method of struts2 which will not be possible if we map action path to Action class in struts.xml

Thanks
Navin
Navin Pillu
Ranch Hand

Joined: Apr 19, 2005
Posts: 103
Hi Jothi,

Thanks a lot for your help. Now i understood the thing properly.

In our struts.xml we have to specify


Setting "struts.objectFactory" to "spring" will force Struts to instantiate the actions using Spring, injecting all the defined dependencies on applicationContext.xml.

And applicationContext.xml will look like


Thanks a lot again
Navin
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Move that constant definition (the object factory definition) to a struts.properties file.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, constant configuration should be kept in the XML file: it's the preferred mechanism. struts.properties is primarily for WebWork backwards compatibility. It could also be kept in the web.xml file if you don't need any Struts 2 configuration files.

http://struts.apache.org/2.x/docs/constant-configuration.html
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

So you mean to say that just for the backward compatibility we have the properties file. Good for letting me know. I will move all of them with immediate effect to my config file... it's after all what the Newton's law says....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Struts2 and Spring integration