Friday, June 13, 2008

Scrollable Label

To make a ready only label using a textfield so that the label is scrollable, use the code below

<html:text style="border:0" name="fop" property="account" size="16" readonly="true" />

Distinguish servlet requests

To find out if a servlet request button action is coming from a forward or not, use this code below

if( instForm.isBtnClose() )
{
// check if this "CLOSE==TRUE" comes from other pages;

if( null==request.getAttribute("javax.servlet.forward.servlet_path") )
{
return mapping.findForward("close");
}
}

Thursday, June 12, 2008

Populate droplist dynamically through AJAX

Mechanism to make an ajax call from one drop list and populate another droplist


In JSP

Add a html select element as below

html:select name=somethingOne id=dropListOne onchange="retrieveDropTwoOptions()

html:select name=somethingTwo id=dropListTwo


function retrieveDropTwoOptions(){

dropListOne = document.getElementById('dropListOne');
if(dropListOne.selectedIndex==0){
document.getElementById('dropListTwo').options.length = 0;
document.getElementById('dropListTwo').options[i] = new Option("---", "", true);
return;
}
dropListOneCode = dropListOne.options[dropListOne.selectedIndex].value;
url='/MyServer/ajax.do?method=populateDropList' +
'&dropListOneCode=' + dropListOneCode ;


//Do the Ajax call
req = ajaxRequestSend(url, "GET", populateDropListTwoBox);

}

//Callback function
function populateDropListTwoBox(){

if (req.readyState == 4) { // Complete
if (req.status == 200) { // OK response
textToSplit = req.responseText;

myCode = document.getElementById('dropListTwo').value; // obtained from form
//Split the document
returnElements=textToSplit.split("\n")

//Process each of the elements
document.getElementById('dropListTwo').options.length = 0;
for ( var i=0; i valueLabelPair = returnElements[i].split(":")
if (myCode == valueLabelPair[0]) {
document.getElementById('dropListTwo').options[i] = new Option(valueLabelPair[1], valueLabelPair[0], true);
}
else {
document.getElementById('dropListTwo').options[i] = new Option(valueLabelPair[1], valueLabelPair[0]);
}
}
}
}


function ajaxRequestSend(url, method, callBackFunciton){
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open(method, url, true);
req.onreadystatechange = callBackFunciton;
req.send(null);
return req;
}


In AjaxAction.java, we add a simple method to return a value

public class AjaxAction extends Action
{
//...

public void populateDropList()
{
StringBuffer body = new StringBuffer(":---\n");

//... get data from database

while (resultSet.next()) {
code = resultSet.getString("code") ;
desc = resultSet.getString("name") ;
body.append("\n").append(code).append(":").append(desc);

}

response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(body.toString());
response.getWriter().flush();

}

}

Simple AJAX

Method to get make a simple ajax call and get a response

function openTermsAndConditions()
{

var selectedIndex = document.getElementById("planOptions").selectedIndex;
var selectedText = document.getElementById("planOptions").options[selectedIndex].text;
if(selectedText)
{
retrievePlanTermsURL(selectedText);
}
}

function retrievePlanTermsURL(planId)
{
url = '/MyServer/ajax.do?method=getPlanTermsAndConditionsURL' +
'&planId=' + planId;

//Do the Ajax call
req = ajaxRequestSend(url, "GET", openURL);

}

//Callback function
function openURL()
{
var termsURL = "";
if (req.readyState == 4) // Complete
{
if (req.status == 200) // OK response
{
termsURL = req.responseText;
document.getElementById("termcondURL").value = termsURL;
if(termsURL)
{
window.open(termsURL, "windowTermsAndConditions", "width=500,height=500");
}
}
}
}

function ajaxRequestSend(url, method, callBackFunciton){
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
req.open(method, url, true);
req.onreadystatechange = callBackFunciton;
req.send(null);
return req;
}


In AjaxAction.java, we add a simple method to return a value

public class AjaxAction extends Action
{
//...

private void getPlanTermsAndConditionsURL() throws Exception
{
String returnValue = DatabaseHelper.getSomeData();
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(returnValue);
response.getWriter().flush();
}


}

In JSP, add an html select element with id =planOptions

Nested Iterate tag

If you have a Collection in ActionForm and require to access them, we can use nested:iterate

<nested:iterate name="memberCustomerViewForm" property="addressList" >
<div>
<span class="leftBuffer"> </span>
<span class="generalInput">
<nested:text property="addressType" onblur="toUpper(this);" size="11" />
<nested:hidden property="addressId"/>
</span>
</nested:iterate>


Note
- do not use id=current in nested:iterate
- don't access like <nested:text name="current" property="addressType" />

Tuesday, May 27, 2008

Dynamically Add or Remove HTML elements

<HTML>
<HEAD>
<script type="text/javascript">

var sequence = 0;

function addTextBox()
{
var dynDiv = document.getElementById('dynamicDiv');
var newDiv = document.createElement("DIV");
var newDivIdName = 'Div'+sequence;
newDiv.setAttribute('id',newDivIdName);
var newSpan = document.createElement('SPAN');

addField(newSpan,{ type :"text", name :"test"+sequence, value:"test"+sequence , size:30 });
addField(newSpan,{ type :"button", name :"delete"+sequence, value:"delete", id:"delete"+sequence });

newDiv.appendChild(newSpan);
dynDiv.appendChild(newDiv);

var deleteButton = document.getElementById('delete'+sequence);
deleteButton.onclick=function()
{
removeTextBox(newDivIdName);
}

sequence++;
}



function addField(form,json)
{
var inp=document.createElement("input");
for(var i in json)
{
inp[i]=json[i];
}
form.appendChild(inp);
}


function removeTextBox(divNum)
{
var dynDiv = document.getElementById('dynamicDiv');
var obj = document.getElementById(divNum);
dynDiv.removeChild(obj);
}

</script>

</HEAD>


<BODY>
<form name="testForm">
<div id="dynamicDiv">
<span>

<input type="text" name="address1" value="test"/>
<input type="button" name="Add" value="Add" onclick="addTextBox();" />
</span>
</div>

</form>
</BODY>

</HTML>

Thursday, May 1, 2008

What Kevin says...

4 yrs

twinkle twinkle little star

how I wonder what you are
appa daddy world so high
like a diamond in the sky

5.5 yrs

shopkeeper: how old are you?
kevin: half and a five

kevin: mum do you eat cows? At school my friend said she ate a cow!