Redirect Result Example
Posted on: January 6, 2011 at 12:00 AM
In this tutorial you will learn how to redirect a request to different application resources

Redirect Result Example

The Redirect Result makes a new call to the resources. It calls the HttpServletResponse.sendRedirect() internally. The reson of doing this means the action error, field error, actions instance are no longer available, they are  lost. Because action is built on single thread-model. There is only one way to pass data is through session or request parameters (?id=userId). To use Redirect Result in your application you need to do following mapping.

<action name="doLogin" class="net.roseindia.Login">
<result name="success" type="redirect">resource Page</result>
<result name="error" type="redirect">error Page</result>
</action>

An Example of Redirect Result is given below

login.jspADS_TO_REPLACE_1

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<head>
<title>Login Application</title> 
</head>

<body bgcolor=lighblue>
<s:form action="login" method="POST"> 
<table border=1 colspacing=5 colspading=5 align=center>
<tr>
<td colspan=3 align=center>
<font color=megenta face=arier>Please Login</font>
</td>
</tr>
<tr>
<td colspan=3 align=center>
<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"/> 
</table>
</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><br>
</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(!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.xmlADS_TO_REPLACE_2

<?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="roseindia" extends="struts-default">

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

<!-- Redirect to another namespace -->

<action name="doLogin" class="net.roseindia.Login">
<result name="success" type="redirect">secure/homePage</result>
<result name="error" type="redirect">error/errorPage</result>
</action>

</package>

<package name="secure" namespace="/secure" extends="struts-default">

<action name="homePage">
<result>/jsp/home.jsp</result>
</action>

</package>

<package name="error" namespace="/error" extends="struts-default">

<action name="errorPage">
<result>/jsp/error.jsp</result>
</action>

</package>

</struts>

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


 
 

Download this example code

Related Tags for Redirect Result Example:

Advertisements

Ads

Ads

 
Advertisement null

Ads