• 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

Struts2: which result type to use

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have execute in the superclass declared as follows:


I warned you that my code above was not exact!
 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
worked
 
Lookout! Runaway whale! Hide behind 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