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 ?
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 ;)
SCJP 5.0, SCWCD 5.0, SCBCD 5.0
subject: returning user defined strings in Struts 2 action classes