Search This Blog

Saturday 14 September 2013

Struts 2 AJAX Demo with jQuery




Struts 2 AJAX Demo with jQuery

src\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.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="resource.application" />
     <constant name="struts.multipart.maxSize" value="1000000" />
    <package name="default" namespace="/" extends="struts-default">
        <result-types>
            <result-type name="tiles"
                class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>
        <action name="landing_page">
            <result name="success" type="tiles">/landing_page</result>
        </action>
        <action name="ajaxRequestForJsp">
            <result name="success"  type="tiles">/ajaxRequestForJsp</result>
        </action>  
        <action name="ajaxRequestForActionClassAndJsp" method="processAjaxRequest" class="com.xyz.abc.actions.AjaxAction">
            <result name="success"  type="tiles">/ajaxRequestForActionClassAndJsp</result>
        </action>
    </package>
</struts>

WEB-INF\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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Struts2Day1</display-name>
    <filter>
        <filter-name>struts</filter-name>

        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
    <context-param>
        <param-name>tilesDefinitions</param-name>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>
</web-app>

WEB-INF\tiles.xml
=============================================================

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE tiles-definitions PUBLIC      
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"      
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="baseLayout" template="/jsp/baselayout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/jsp/header.jsp" />
        <put-attribute name="menu" value="/jsp/menu.jsp" />
        <put-attribute name="contentbody" value="" />
        <put-attribute name="footer" value="/jsp/footer.jsp" />
    </definition>
    <definition name="singleLayout" template="/jsp/singlelayout.jsp">
        <put-attribute name="divcontent" value="" />
    </definition>
    <definition name="/landing_page" extends="baseLayout">
        <put-attribute name="title" value="Landing Page" />
        <put-attribute name="contentbody" value="/jsp/welcome.jsp" />
    </definition>
    <definition name="/ajaxRequestForJsp" extends="singleLayout">
        <put-attribute name="divcontent" value="/jsp/ajaxJspResponse.jsp" />
    </definition>
    <definition name="/ajaxRequestForActionClassAndJsp" extends="singleLayout">      
        <put-attribute name="divcontent" value="/jsp/ajaxActionsClassAndJspResponse.jsp" />
    </definition>
</tiles-definitions>

src\log4j.properties
===========================================================

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${jboss.server.home.dir}/log/struts2Ajax.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Root logger option
log4j.rootLogger=INFO, file, stdout

# Log everything. Good for troubleshooting
log4j.logger.org.hibernate=INFO

# Log all JDBC parameters
log4j.logger.org.hibernate.type=ALL

com\xyz\abc\actions\AjaxAction.java
===================================================

package com.xyz.abc.actions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;


/**
 * @author Pedababu M
 *
 */
public class AjaxAction extends ActionSupport {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private String actionClassName;
    private String methodName;
    final Logger LOG = LoggerFactory.getLogger(AjaxAction.class);

  
    public String processAjaxRequest() {
        LOG.info("AjaxAction processAjaxRequest start");
        //Include application specific logic
        this.actionClassName="AjaxAction.java";
        this.methodName="processAjaxRequest";
        LOG.info("AjaxAction processAjaxRequest end");
        return SUCCESS;
    }


    /**
     * @return the actionClassName
     */
    public String getActionClassName() {
        return actionClassName;
    }


    /**
     * @param actionClassName the actionClassName to set
     */
    public void setActionClassName(String actionClassName) {
        this.actionClassName = actionClassName;
    }


    /**
     * @return the methodName
     */
    public String getMethodName() {
        return methodName;
    }


    /**
     * @param methodName the methodName to set
     */
    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }
  
}

css\ajaxdemocss.css
=======================================================

@charset "utf-8";
.text {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #333333;
    text-decoration: none;
  
}
.textbox {
    BACKGROUND-COLOR: #ffffff;
    BORDER-BOTTOM: #455f55 1px solid;
    BORDER-LEFT: #455f55 1px solid;
    BORDER-RIGHT: #455f55 1px solid;
    BORDER-TOP: #455f55 1px solid;
    COLOR: #000000;
    FONT-FAMILY: verdana, arial;
    FONT-SIZE: 11px;
    FONT-WEIGHT: normal;
    height: 25px;
    WIDTH: 70px
}

.filltextbox {
    BACKGROUND-COLOR: #ffffff;
    BORDER-BOTTOM: #455f55 1px solid;
    BORDER-LEFT: #455f55 1px solid;
    BORDER-RIGHT: #455f55 1px solid;
    BORDER-TOP: #455f55 1px solid;
    COLOR: #000000;
    FONT-FAMILY: verdana, arial;
    FONT-SIZE: 11px;
    FONT-WEIGHT: normal;
    height: 25px;
    WIDTH: 40px
}
.button {
    BORDER-RIGHT: #999999 1px solid; BORDER-TOP: #999999 1px solid;
    FONT-WEIGHT: bold; FONT-SIZE: 11px; BORDER-LEFT: #999999 1px solid;
    COLOR: #666666; BORDER-BOTTOM: #999999 1px solid;
    FONT-FAMILY: Arial, Helvetica, sans-serif; WIDTH: 100px; BACKGROUND-COLOR: #cccccc
}

.imgtagborder {
    border-style: none
}

.boxhead {
    color: #000000;
    text-decoration: none;
}
.border {
    BACKGROUND-COLOR: #ffffff;
    BORDER-BOTTOM: #455f55 1px solid;
    BORDER-LEFT: #455f55 1px solid;
    BORDER-RIGHT: #455f55 1px solid;
    BORDER-TOP: #455f55 1px solid;
    COLOR: #000000;
}
.selectborder {
    background: url(../images/select_border.jpg) repeat-x top;
    BORDER-BOTTOM: #455f55 1px solid;
    BORDER-LEFT: #455f55 1px solid;
    BORDER-RIGHT: #455f55 1px solid;
    BORDER-TOP: #455f55 1px solid;
    border:1px solid;
}

.boxinside_selectrightVM {
line-height:1em;
margin-right:1em;
margin-top:1em;
}
.mttdfontstyle {
    color:#000000;
    font-weight: bolder;
}
.mtheadtdfontstyle {
    color:#FFFFFF;
    font-weight: bolder;
}
.tableborder{
    border: 1px solid black;
}
.divminheight {
    min-height: 20px;
    height: 100%;
}

javascript\ajax_javascript.js
==========================================================

function jspRequest() {
    $.ajax({ url: "ajaxRequestForJsp",
             //data: "request_param_name=" + request_param_name value,
              success: function(data) {  
            //alert(data);
              document.getElementById('responseDiv').innerHTML = data;
             }
    });
  
}

function ajaxRequestForActionClassAndJsp() {
    $.ajax({ url: "ajaxRequestForActionClassAndJsp",
             //data: "request_param_name=" + request_param_name value,
              success: function(data) {  
            //alert(data);
              document.getElementById('responseDiv').innerHTML = data;
             }
    });
  
}

WebContent\index.jsp
================================================================

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
    <center><a href="landing_page.action">Click here to get AJAX Demo</a></center>
</body>
</html>

WebContent\jsp\ajaxActionsClassAndJspResponse.jsp
=====================================================================

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>

<center><h1>Response of action class</h1></center><br/>

<center>Action Class Name: <s:property value="actionClassName" /></center><br/>
<center>Action Class method Name: <s:property value="methodName" /></center><br/>

WebContent\jsp\ajaxJspResponse.jsp
================================================================

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<center><h1>Ajax JSP Response - displaying</h1></center>
<%-- Include JSP logic --%>

WebContent\jsp\baselayout.jsp
==================================================================

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" /></title>
<link href="css/ajaxdemocss.css" rel="stylesheet" type="text/css" />
<SCRIPT LANGUAGE="JavaScript" src="javascript/ajax_javascript.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" src="javascript/jquery-1.8.1.js"></SCRIPT>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" align="center">
    <tr height="15%">
        <td><tiles:insertAttribute name="header" />
        </td>
    </tr>
    <tr height="75%">
    <td>
        <TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
        <TR>
            <td height="80%" width="10%"><tiles:insertAttribute name="menu" /></td>
            <td width="90%">
                <table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
                <tr height="70%"><td><tiles:insertAttribute name="contentbody" /></td></tr>
                </table>
            </td>
        </TR>
        </TABLE>
    </td>      
    </tr>
    <tr height="10%">
        <td><tiles:insertAttribute name="footer" />
        </td>
    </tr>
</table>
</body>
</html>

WebContent\jsp\content1003.jsp
===============================================================

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
  <TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
    <TR height="100%">
    <TD>
    <DIV id="pageData">&nbsp;</DIV>
    </TD>
    </TR>
  </TABLE>

WebContent\jsp\footer.jsp
====================================================================

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
 <TR>
    <TD><center>&copy;Babu Tech Stuff</center></TD>
 </TR>
</TABLE>

WebContent\jsp\header.jsp
=====================================================================

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<table width="100%" height="100%" cellspacing="0" cellpadding="0">
    <tr height="50%">
        <td width="10%">&nbsp;</td>
        <td width="75%">&nbsp;</td>
        <td width="15%"><span style="float: right"><u>Sign Out</u></span></td>
    </tr>
    <TR height="50%">
     <TD colspan="3">
        <div class="divminheight">
         <TABLE WIDTH="100%"   cellspacing="0" cellpadding="0">
             <TR>
                <TD bgcolor="#EFEDF1">&nbsp;Header</TD>
                <TD bgcolor="#EFEDF1" align="right">&nbsp;</TD>
            </TR>
        </TABLE>
        </div>
    </TD>
    </TR>
</table>

WebContent\jsp\menu.jsp
=================================================================

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<TABLE border="0" cellpadding="0" cellspacing="0" width="100%"
    height="100%">
    <TR height="5%">
        <TD>&nbsp;</TD>
    </TR>
    <TR height="5%">
        <TD>
        <CENTER><A href="#" onClick="jspRequest();">
        <b>Click For Ajax Request Jsp Response</b></A></CENTER>
        </TD>
    </TR>
    <TR height="5%">
        <TD>&nbsp;<b>================</b></TD>
    </TR>
    <TR height="5%">
        <TD>
        <CENTER><A href="#"
            onClick="ajaxRequestForActionClassAndJsp();">
            <b>Click For Ajax Request Action Class And Jsp Response</b></A></CENTER>
        </TD>
    </TR>
    <TR height="90%">
        <TD>&nbsp;</TD>
    </TR>
</TABLE>

WebContent\jsp\singlelayout.jsp
=================================================================

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<tiles:insertAttribute name="divcontent" />

WebContent\jsp\welcome.jsp
==================================================================

<table border="0" cellpadding="0" cellspacing="0" width="100%"
    height="100%">
    <tr>
        <td>
        <div id="responseDiv" style="display: block">
        <center>
        <h1>&nbsp;Welcome to AJAX Demo with jQuery</h1>
        </center>
        </div>
        </td>
    </tr>
</table>

No comments:

Post a Comment