Introduction to PreResultListener
Posted on: January 21, 2011 at 12:00 AM
In this tutorial you will learn about the PreResultListener interface

Introduction To PreResultListener

PreResultListener is an interface of com.opensymphony.xwork2.interceptor package. It may provide a way to get register with ActionInvocation. It is executed after the action execution but before the result. It provide a method beforeResult(ActionInvocation invocation, String resultCode). This method is called after the action execution but before the result execution. An example of PreResultListener is given below

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=loginPage.action">
</head>

<body bgcolor="lightgreen">
<center><font color="black" face="modern" size=6>Loading ...</font></center>
</body>
</html>

login.jspADS_TO_REPLACE_1

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

<head>
<title>Pre Result Listener Example</title> 
</head>

<body bgcolor="#F6CEE3">
<s:form action="preResult"> 
<table border=1 colspacing=5 colspading=5 align=center>
<tr>
<td colspan=3 align=center>
<font color="#01DF01" face=arier>Please Enter Your Name</font>
</td>
</tr>
<tr>
<td colspan=3 align=center>
<s:actionerror />
<s:fielderror />
</td>
</tr>
<s:textfield name="name" label="Please Input Id"/>
<s:submit value="Login" align="center"/> 
</table>
</s:form>

</body>
</html>

home.jsp


<%@ taglib prefix="s" uri="/struts-tags" %><HTML>
<HEAD>
<TITLE> Home Page </TITLE>
</HEAD>

<body bgcolor="#F6E3CE">
<table border=1 colspacing=5 colspading=5 align=center>
<tr>
<td colspan=2 align="center">
<h1>You Are Welcome</h1>

</td> 
</table>
</body>
</HTML>

error.jsp

<%@ taglib prefix="s" uri="/struts-tags" %><HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Error Page</TITLE>
</HEAD>

<BODY bgcolor="lightblue">
<center><h3>Sorry Wrong User Id</h3><h2> <s:property value="name"/></h2>
<h3>Please Input User Id roseindia</h3></center>
</BODY>
</HTML>

PreResultListenerExample.javaADS_TO_REPLACE_2

package net.roseindia;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.PreResultListener;

public class PreResultListenerExample extends ActionSupport{
	private static final long serialVersionUID = 1L;
	public String control=INPUT;
	
	String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String execute() throws Exception {
		 ActionContext context=ActionContext.getContext();
         ActionInvocation invocation=context.getActionInvocation();
         invocation.addPreResultListener(new PreResultListener() {
        	   public void beforeResult(ActionInvocation invocation, String resultCode){
                  // perform operation necessary before Result execution
            	  System.out.println("Executing Action - "+invocation.getAction());
            	     if(getName().equalsIgnoreCase("roseindia")){
            		  System.out.println(getName());
            		  invocation.setResultCode(SUCCESS);
            	  }
            	  else{
            		  System.out.println("Going to error page "+getName());
            		  invocation.setResultCode(ERROR);
            	  }
            	}
            });                 
		return control;
     }
  }
  

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

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

<action name="preResult" class="net.roseindia.PreResultListenerExample">
<result name="error">/jsp/error.jsp</result>
<result name="input">/jsp/login.jsp</result>
<result name="success">/jsp/home.jsp</result>
</action>

</package>

</struts>
When you run this application it will display message as shown below:

 
 

Download this example code

Related Tags for Introduction to PreResultListener:

Advertisements

Ads

Ads

 
Advertisement null

Ads