• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can Sturts Action classes have constructors?

 
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can Sturts Action classes have constructors(default or parameterized)?


Thanks in advance.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can create as many constructors as you like for an Action class. It follows the same rules for constructors as any other Java class.

BUT

Since Struts instantiates an Action class and not you, the only constructor that will ever be used is the one with no arguments. If you make it so the class has no public, no-arguments constructor, Struts will throw an exception when it tries to instantiate the class.
 
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a related note...Keep in mind that in Struts 1.x Action class instances are shared between users and must be thread safe. In general you should not create member variables in your action classes (though I do sometimes have ones declared as final where the value would be the same for every instance of the Action).

- Brent
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.
I have an other question:
I am using DispatchAction which has 5 methods. All the 5 methods have some generic code(getting manager(s) instance thru spring) .

I don't want this generic code to be scattered in all the methods. which is the best approach to do this?

Thanks in advance
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can think of at least 2 possible solutions:

1- put the common code in a method that is called from each of the other methods.

2- Override the execute(..) method and put your common code There. If the manager you're tring to initialize is the same for all users, you can put it in an instance variable that is accessible from all methods. Make sure that you end the method by calling super.execute(...) which will then cause DispatchAction to route to the appropriate method.
[ November 05, 2007: Message edited by: Merrill Higginson ]
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think didn't explain my problem clearly in my previous post. Here it is

I have these lines of code in all 5 methods in my dispatch action class



Instead of writing these 4 lines of code in every method can i declare them as instance variables. I don't have to worry about thread safety because these are singleton beans and are created by spring.
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as exception handling is not an issue you should be able to set these in a parameter-less constructor. Like this:



- Brent
 
Pay attention! Tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic