• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Struts Spring simple example

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added the struts2-spring-plugin-2.0.11.2.jar to the classpath.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. (I'd really recommend upgrading your S2 version, but that's a different issue.)
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic