Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts2 and Spring integration

 
Navin Pillu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check this http://www.coderanch.com/t/472077/Struts/Struts-Tomcat
 
Joe Harry
Ranch Hand
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consult the Spring reference documentation for a broader understanding.
 
Navin Pillu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is that DelegationActionProxy?? Write your action class and map that in your struts.xml file.
 
Joe Harry
Ranch Hand
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better spend some time reading the action mapping definitions of Struts 2.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you're trying to use Spring's Struts 1 support. Don't do that.
 
Navin Pillu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get rid of that DelegationContext!
 
Navin Pillu
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Move that constant definition (the object factory definition) to a struts.properties file.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic