wood burning stoves 2.0*
The moose likes Struts and the fly likes using inheritance in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "using inheritance in struts" Watch "using inheritance in struts" New topic
Author

using inheritance in struts

rangababu chakravarthula
Greenhorn

Joined: Jan 02, 2004
Posts: 7
Hi

Is is possible to have a action form bean which will have common form elements and have separate action form beans for each kind of report that will inherit the base action form and use the form elements.

If yes, do I have to have the base action form entry in struts-config.xml

Same question with Action class. Can I have a base action class and have report specific action classes that inherit the base action class. because in my case for every report that the application needs to generate it has to check roles that will be available in request cookies. (jaas roles is not possible in this case. has to be request cookies) can I have that functionality in base action class and have report specific actions in their respective action classes. And also what configuration needs to be done in struts-config.xml

thanks
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Yes, it certainly is possible to use inheritance for both an ActionForm bean and an Action class.

For an action form, the process is:
  • Write your base class with the shared properties and their getters and setters. This class should extend ActionForm
  • Write your subclasses extending your base class with the added properties and getters and setters
  • Make an entry in struts-config.xml for each subclass. There is no need to put the base class anywhere in struts-config.xml


  • For an Action class, the process is:
  • Write your base class extending Action. Your execute method should contain logic that is performed for all actions. My suggestion is to make it return null if everything is OK, and have it return an ActionForward to an error page if it encounters an error.
  • Write your subclasses extending your base class. The execute method should start with:

  • ActionForward forwardFromBase = super.execute(<list all parms>);
    if (forwardFromBase != null) return forwardFromBase():
  • The rest of the execute() method should contain logic unique to each subclass
  • Make an entry in struts-config.xml for each action. There is no need to put the base class anywhere in struts-config.xml


  • Good luck!
    [ June 10, 2005: Message edited by: Merrill Higginson ]

    Merrill
    Consultant, Sima Solutions
    rangababu chakravarthula
    Greenhorn

    Joined: Jan 02, 2004
    Posts: 7
    great! thank you very much Merrill. Will try out a sample and test.
    amit gupta
    Greenhorn

    Joined: Dec 29, 2006
    Posts: 23
    rangababu chakravarthula wrote:great! thank you very much Merrill. Will try out a sample and test.


    I have tried to extend action form. But when I try to access the properties of base class in sub class. it gives me null.


    SCJP 1.5, SCWCD 5.0
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: using inheritance in struts
     
    Similar Threads
    Common base class for classes extending Action and DispatchAction Classes
    Tracking User's requests.
    How to have Common base class for classes extending Action and DispatchAction Classe
    My understanding of Struts vs Tapestry
    struts control problem..