Wednesday, February 19, 2014

Apache Struts 2: Redirecting to a link and another Action

Apache Struts is a project developing an open-source web application framework for Java EE web application development that promotes a model–view–controller (MVC) architectural approach through extensions the Java Servlet web API. [Definition from Wikipedia]

To know more about Apache Struts 2, click here.

Redirect to JSP page

It is very simple to redirect to a JSP(JavaServer Pages) which is a technology that helps software developers create dynamically generated web pages based on HTML, XML, or other document types.

<action name="login" class="LoginAction">

<result name="input">login.jsp</result>

</action>

Redirect to Action

Assuming that you have an Action called LoginAction and HomeAction, 

<action name="login" class="LoginAction">

<result name="success" type="redirectAction">

<param name="actionName">home</param>

</result>

</action>


<action name="home" class="HomeAction">

<result name="success">home.jsp</result>

</action>


You can also pass parameters to an action while redirecting and call another Page if the input are not provided in the initial page like this.

<action name="login" class="LoginAction">
<result name="success" type="redirectAction">
<param name="actionName">home</param>
<param name="userName">${userName}</param>
</result>
<result name="input">login.jsp</result>
</action>

Description:

If LoginAction returns "success" then go to "home" action (which calls HomeAction).
If input values are not properly provided to LoginAction and it returns "input" or "Action.INPUT", then open login.jsp.