# 4
well as far your requirement is concern if at all you are planning to implement AJAX try to use the below link which might be of some help...
http://www.it-eye.nl/weblog/2005/12/13/ajax-in-struts-implementing-dependend-select-boxes/
However,I somehow feel there are few loopholes in the author's approach...
i advice to use XmlHttpRequest.reponseXML property there.
However, i've mentioned a sample code snippet for you reference.
XML Response Pattern :
======================
<? xml version="1.1" ?>
<dropdown>
<option>
<val>CUSTOMIZED_VALUE</val>
<text>CUSTOMIZED_VALUE</text>
</option>
.........
.........
.........
.........
</dropdown>
Sample.jsp:
===========
<%@page language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Automatic Drop-Down Updation</title>
<script language="javascript">
// Global Variable for XmlHttp Request Object
var xmlhttp
// Timer Variables
var c = 0
var t
/* A function which calls a servlet named AjaxServlet to get XmlData using XmlHttpObject */
function refreshCombo(txt){
xmlhttp = null
// code for initializing XmlHttpRequest Object On Browsers like Mozilla, etc.
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest()
}
// code for initializing XmlHttpRequest Object On Browsers like IE
else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp != null){
// Setting the Action url to get XmlData
url = "dropdown.do?count="+txt;
// Course of Action That Should be Made if their is a change in XmlHttpRequest Object ReadyState NOTE : it is 4 when it has got request from CGI
xmlhttp.onreadystatechange = getResponseAction;
// Open the Request by passing Type of Request & CGI URL
xmlhttp.open("GET",url,true);
// Sending URL Encoded Data
xmlhttp.send(null);
}
else{
// Only Broswers like IE 5.0,Mozilla & all other browser which support XML data Supports AJAX Technology
// In the Below case it looks as if the browser is not compatiable
alert("Your browser does not support XMLHTTP.")
}
}
/* Used for verifing right ReadyState & Status of XmlHttpRequest Object returns true if it is verified */
function verifyReadyState(obj){
// As Said above if XmlHttp.ReadyState == 4 then the Page Has got Response from WebServer
if(obj.readyState == 4){
// Similarly if XmlHttp.status == 200 it means that we have got a Valid response from the WebServer
if(obj.status == 200){
return true
}
else{
alert("Problem retrieving XML data")
}
}
}
/* Action that has to take place after getting reponse */
function getResponseAction(){
// Verifying State & Status
if(verifyReadyState(xmlhttp) == true){
// Building a DOM parser from Response Object
var response = xmlhttp.responseXML.documentElement
// Deleting all the Present Elements in the Drop-Down Box
drRemove()
// Checking for the Root Node Tag
var x = response.getElementsByTagName("option")
var val
var tex
var optn
for(var i = 0;i < x.length; i++){
optn = document.createElement("OPTION")
var er
// Checking for the tag which holds the value of the Drop-Down combo element
val = x[i].getElementsByTagName("val")
{
try{
// Assigning the value to a Drop-Down Set Element
optn.value = val[0].firstChild.data
} catch(er){
}
}
// Checking for the tag which holds the Text of the Drop-Down combo element
tex = x[i].getElementsByTagName("text")
{
try{
// Assigning the Text to a Drop-Down Set Element
optn.text = tex[0].firstChild.data
} catch(er){
}
}
// Adding the Set Element to the Drop-Down
document.SampleForm.SampleCombo.options.add(optn)
}
}
}
/* Function removes all the elements in the Drop-Down */
function drRemove(){
var x = document.SampleForm.SampleCombo
for(var i = document.SampleForm.SampleCombo.length - 1 ; i >= 0 ; i--){
x.remove(i)
}
}
</script>
</head>
<body onload="syncCount()">
<pre> <h1>Refresh Drop-Down <div id='txt'> </div> </h1></pre>
<form name="SampleForm">
<!-- Drop Down which has country list -->
<select name="x" onchange="refreshCombo(this.value)">
<option value="1">United States</option>
<option value="2">United Kingdom</option>
<option value="3">United Arab Emriates</option>
................
................
</select>
<!-- Drop Down which is dependent on Country Drop down get list of states -->
<select name="SampleCombo">
<option value="-1">Pick One</option>
</select>
</form>
</body>
</html>
struts-config.xml:
==================
<action-mappings>
<action path="/dropdown" type="com.controlleraction.AjaxActionClass">
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>
AjaxActionClass.java:
=====================
package com.controlleraction;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class AjaxActionClass extends DispatchAction {
public ActionForward execute( ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception {
String req = new String("");
try{
req = request.getParameter("count");
} catch(Exception exp){
}
if(!req.equals("")){
response.setContentType("text/xml");
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache,post-check=0,pre-check=0");
PrintWriter out = response.getWriter();
/*a sample bean where we trying to call a service from Model*/
com.Biz.XmlBean xml = new XmlBean();
String buffer = xml.getXmlData(req);
if(xml.close() == true && buffer.equals("") == false)
out.write(buffer);
return(null);
} else {
return new ActionForward("error");
}
}
}
XmlBean.java:
=============
/*
* XmlBean.java
*
*/
import java.sql.*;
import java.util.*;
import java.io.*;
/**
*
* @author RaHuL
*/
public class XmlBean {
private Connection con = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// Setting CLASSURL path to TYPE I Driver
private String CLASSURL = "sun.jdbc.odbc.JdbcOdbcDriver";
/* Specifing CONNECTION PATH to a DSN named TestDsn
* Please Make Sure you create a DSN Named TestDsn to your database which holds EMP table
*/
private String CONNECTIONURL = "jdbc:odbc:TestDsn";
boolean IS_ESTABLISHED = false;
/** Creates a new instance of XmlBean and also establishes DB Connections */
public XmlBean() {
try{
Class.forName(CLASSURL);
con = DriverManager.getConnection(CONNECTIONURL,"admin","");
IS_ESTABLISHED = true;
} catch(SQLException sqe){
sqe.printStackTrace();
} catch(Exception exp){
exp.printStackTrace();
}
}
/* Generates XmlData For the Business Logic Specified */
public String getXmlData(String req){
String XmlBuffer = new String("");
if(IS_ESTABLISHED == true){
try{
pstmt = con.prepareStatement("SELECT stateid,statename FROM STATE_TABLE where countryid = ?");
pstmt.setString(1,req);
rs = pstmt.executeQuery();
if(rs != null){
XmlBuffer = XmlBuffer + "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
XmlBuffer = XmlBuffer + "<!-- Edited by Rahul Sharma -->";
// Root Node
XmlBuffer = XmlBuffer + "<dropdown>";
while(rs.next()){
String value = rs.getString(1);
String text = rs.getString(2);
// Sub-root Node
XmlBuffer = XmlBuffer + "<option>";
// node which holds value of drop-down combo
XmlBuffer = XmlBuffer + "<val>"+value+"</val>";
// node which holds text for drop-down combo
XmlBuffer = XmlBuffer + "<text>"+text+"</text>";
XmlBuffer = XmlBuffer + "</option>";
}
XmlBuffer = XmlBuffer + "</dropdown>";
}
}catch(SQLException sqe){
sqe.printStackTrace();
} catch(Exception exp){
exp.printStackTrace();
}
}
return(XmlBuffer);
}
/* Closes the DB Connection Conmpletely */
public boolean close(){
if(IS_ESTABLISHED == true){
try{
pstmt.close();
con.close();
return(true);
} catch(SQLException sqe){
sqe.printStackTrace();
} catch(Exception exp){
exp.printStackTrace();
}
}
return(false);
}
}
NOTE: I understand i'm not completely coded things as per proper coding standards.please execuse me for that as this example was just given to enable user to learn how XmlHttpRquest,reponseXML
can be used for better purposes instead of devising manual parsing.
where i've used XmlHttpResponse pattern to be in XML. you may make use of other practices like JSON & so on depending on your requirement..
and and if you are more instrested in integrating Struts with AJAX using few frameworks & customized tag based support please go though the below link.
http://struts.sourceforge.net/ajaxtags/index.html
Hope that might help :)
REGARDS,
RaHuL