Wednesday, August 15, 2012

STRUTS 2 programs 009 PreResultListener

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 kites</h3></center>
</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>

-------------------------------------------------------------------------------------------------------------------------------
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.jsp
------------

<%@ 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>
----------------------------------------------------------------------------------------------------------------------------
PreResultListenerExample.java
-------------------------------------------------

package com.kites;

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("kites")){
                      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="com.kites.PreResultListenerExample">
<result name="error">/error.jsp</result>
<result name="input">/login.jsp</result>
<result name="success">/home.jsp</result>
</action>

</package>

</struts>
---------------------------------------------------------------------------------------------------------------------------------
web.xml
--------------
<?xml version="1.0" encoding="UTF-8"?>

<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">

<display-name>Struts 2</display-name>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
--------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment