Home Struts Struts Struts2.2.1 Redirect Action Result Example

Ask Questions?

View Latest Questions


 
 

Redirect Action Result Example
Posted on: January 7, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn how redirect action on different resources

Redirect Action Result Example

The Result uses the ActionMapper of the ActionMapperFactory for redirecting the URL to the specified action. To redirect the action to the specified location you need to do mapping in the struts.xml  as follows

<action name="redirectAction" class=".....">
<result name="success" type="redirectAction">/jsp/home.jsp</result>
</action>

An example of Redirect Action is given below-

login.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Chain Result Example</title>

<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css"/>

</head>
<body>
<s:form action="doLogin" method="POST"> 
<tr>
<td colspan="2">
Login
</td>
</tr>
<tr>
<td colspan="2">
<s:actionerror />
<s:fielderror />
</td>
</tr>
<s:textfield name="username" label="Login name"/>
<s:password name="password" label="Password"/>
<s:submit value="Login" align="center"/> 
</s:form>
</body>
</html>

home.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body >
<center>
<h1> Home Page</h1>
<font color=green size=15 face=modern> Welcome </font>
</center>
</body>
</html> 

error.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
<body >
<center>
<h1> Error Page</h1>
</center>
</body>
</html>

Login.java

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
public  class Login  extends ActionSupport {

    public String execute() throws Exception {
		System.out.println("Login Action Called");
		if(getUsername().equalsIgnoreCase("")|| getPassword().equalsIgnoreCase("")){
			return ERROR;
		}
		else if(!getUsername().equals("Admin") || !getPassword().equals("Admin")){
            return ERROR;
		}else{
			return SUCCESS;
		}
	}

private String username = null;

public String getUsername() {
        return username;
    }

    public void setUsername(String value) {
        username = value;
    }

    private String password = null;

    public String getPassword() {
        return password;
    }

    public void setPassword(String value) {
        password = value;
    }

}

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="public" extends="struts-default">

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

<action name="doLogin" class="net.roseindia.Login">
<!-- Redirect to another namespace -->
<result name="success" type="redirectAction">
<param name="actionName">homePage</param>
<param name="namespace">/secure</param>
</result>

<result name="error" type="redirectAction">
<param name="actionName">errorPage</param>
<param name="namespace">/error</param>
</result>

</action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
<!-- Redirect to an action in the same namespace -->
<action name="homePage">
<result >/jsp/home.jsp</result>
</action>
</package>

<package name="error" extends="struts-default" namespace="/error">
<!-- Redirect to an action in the same namespace -->
<action name="errorPage">
<result >/jsp/error.jsp</result>
</action>
</package>

</struts>

When you run this application it will display message as shown below:




 
 
 
 

Download this example code

Related Tags for Redirect Action Result Example:


DMCA.com