aspose file tools*
The moose likes Struts and the fly likes Struts2: which result type to use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2: which result type to use " Watch "Struts2: which result type to use " New topic
Author

Struts2: which result type to use

Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
Hello

I have a jsp page. It is called from two (cat and dog) different struts 2 actions. a lot of stuff needs to be done before control comes to this page.

I am wondering what is the best result type I should choose.

Should i do 'redirect action result' type. so lets say the page name is bone.jsp

now two struts classes cat and dog both come to Bone action. and Bone action class 'stuffToDoBeforePage' will contain everything needed to be done before bone.jsp is shown.

Is there a better approach to this problem?
Thanks!!


omnipresent
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

Originally posted by Anil Kumble:
now two struts classes cat and dog both come to Bone action. and Bone action class 'stuffToDoBeforePage' will contain everything needed to be done before bone.jsp is shown.


That doesn't sound too terrible, though it sounds like a struts 1 way to go about it.
Have you thought about using an interceptor that both cat and dog would be configured to use?
How about making cat and dog both subclasses of a pet class that contains stuffToDoBeforePage?


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
but both cat and dog are extending ActionSupport. Should the pet class extend ActionSupport and then cat and dog extend pet class and have their overwrite the 'execute()' method? then when both cat and dog are about to pass control over to bone.jsp they call the method in pet class which does 'stuffToDoBeforePage'.

Thanks for help
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

That sounds like it would work fine.
I usually do something like this (not exact Java, error handling overlooked):

This gives you a little insurance that you won't accidentally delete the "stuffToDoBeforePage" invocation in the subclass and kill your app, plus it's a little more "object oriented" in that the subclass doesn't know much about the superclass.
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
Thanks, that looks much better and very OO.

Have one last question though. I assume an abstract class can only 'define' methods and not write their body right? or i might be wrong.

If I am right then I'll have to write doSuperclassTask in both of my subclasses. and it will basically contain the same thing.

If I'm wrong (i hope i am) then this will work great.

I know, I'm forgetting my basic java OO principals here

edit;
just saw that you declared subclasstask as abstract. so methods with keyword 'abstract' wont have a body. others can.
[ December 10, 2007: Message edited by: Anil Kumble ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

Originally posted by Anil Kumble:

just saw that you declared subclasstask as abstract. so methods with keyword 'abstract' wont have a body. others can.


Correct.

Also abstract methods MUST be overridden by subclasses (it's a compiler error if you don't). This is a handy way to remember to override doSubclassTask method when you create future classes.
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
but now i am getting errors from struts 2 like this



will I have to change my struts.xml as well?

right now my struts.xml is still calling cat and dog classes
for example

thanks for your kind help
[ December 10, 2007: Message edited by: Anil Kumble ]
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8903
    
    8

Do you have execute in the superclass declared as follows:


I warned you that my code above was not exact!
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
yes, I have the following code in my superclass

But still getting that error. should I have superclass somewhere in my struts.xml or some configuration needed in struts.xml?

edit:
did not have abstract class as 'public' doing that now. will let you know
[ December 10, 2007: Message edited by: Anil Kumble ]
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
worked
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2: which result type to use