Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning user defined strings in Struts 2 action classes

 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
How can i achieve the following?
As we know Struts 2 action classes have an execute method which returns a string.In my action class execute method, I call a method which returns a page name as a string.. the page name will be different as per the business logic.

How can i return the page string and map it in struts.xml ?

Thanks in advance.
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand your problem. Struts doesn't care what you return from execute as long as it matches a result in struts.xml and Struts doesn't care what you name results.
 
ryan sukale
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajiv,

Yes, you can return dynamic pages from your struts 2 action. The idea here would be to save the page name in an instance variable in the action, and create a getter/setter for it. Then in your action mapping, you can use the above created getter for your page name.

I created some quickie dummy actions and results just for elaboration purpose. So the naming might look a little bit eerie. Just bear with it.

Check out the following code for the action class.


I will be invoking the "anotherMethod" function in my action mapping that follows. If you wish to, you can use the the code written in the "anotherMethod" function inside the execute method itself.
Here's the action mapping.



As you can see, i returned a "success" string from my "anotherMethod" in the action class. So, the above result will be invoked on completion of the action. The page to be displayed is evaluated as an EL variable and fetches the nextPage property on the DummyAction2 class
The action invocation is just as usual.



I guess, you can let your business logic determine the page name. And you page will be displayed accordingly based upon the value set in a field in the action.

Hope this helps ;)

Regards,
Ryan Sukale
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic