aspose file tools*
The moose likes Struts and the fly likes Struts Spring simple example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Spring simple example" Watch "Struts Spring simple example" New topic
Author

Struts Spring simple example

Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
I've done some searching and followed examples and I am trying to inject into a struts action a simple string using spring DI and I am unsuccessful.

I've got

defined for all requests for the struts filter
in web.xml

I've got

defined as a listener in web.xml

my struts in action is


has field aString with setter,



it is defined is struts.xml on the classpath


and in applicationContext I have the bean wired up:



I would have thought the property name should have been 'aString' but the deployer complained

inside the execute method of the action, aString is null. Nothing was injected. No run time errors at the console. The setter is there. Do I need to access the applicationContext and do a get by id name? I thought the spring container using the context xml would set the string for me when it created the action. I also found something talking about a struts plug-in for configuring spring but I suspect that was for earlier versions of spring and struts and is now deprecated or do I still need that?

Mike

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What made you think you didn't need the Spring plugin to use Spring? Struts 2 needs to know to use Spring as the object factory (and a few other minor things).
Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
Dave or anyone else for that matter, I've done some more searching and I want to confirm that I've found the right path to go down to resolve this...
Looking at
http://www.javabeat.net/articles/70-integrating-struts-with-spring-1.html
to use the ContextLoaderPlugin
The article talks about doing one of 2 things (I have a choice)

A) Overriding the Struts RequestProcessor with Spring’s DelegatingRequestProcessor.
B) Delegate Struts Action management to the Spring framework.

In the struts.xml file, I declare a plugin and provide as a parameter the location of applicationContext



for overriding the struts request processer add

to struts.xml

or in the second way, I set the type in my struts action

which gives the spring container control over creating the object. The action will need to be declared in applicationContext.
Does this sound correct. I thought this would be easier than this. The examples of struts to spring I had seen previously did not include any of this.

Mike

Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
Ok,
I got around this. I did 2 things to fix this and I don't know which did what. I went and download a spring to struts integration plugin. Then I made sure to identify the class in struts.xml to what the id name for the bean was in applicationContext.

From my research, the ContextLoaderPlugin was for Struts 1 being that it worked with the ActionServlet.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Whoa, yeah, you were trying to mix Struts 1 and Struts 2 there.

Just to confirm: you're using the Struts 2 Spring integration plugin from the Struts 2 distribution now, right?
Michael Sampson
Ranch Hand

Joined: Sep 24, 2008
Posts: 30
I added the struts2-spring-plugin-2.0.11.2.jar to the classpath.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Okay. (I'd really recommend upgrading your S2 version, but that's a different issue.)
 
 
subject: Struts Spring simple example