13 December 2009

submit form when Enter button is pressed

Use onkeypress attribute of all the elements in the current form (for which you intend to exploit Enter button’s hit to execute some action) by assigning a javascript method as shown below:

onkeypress="return submitEnter(Id_of_button_to_click_on_enter, event);"


where submitEnter method is as defined below:

function submitEnter(commandId,e) {

var keycode;

if (window.event)

keycode = window.event.keyCode;

else if (e)

keycode = e.which;

else

return true;

if (keycode == 13) {

document.getElementById(commandId).click();

return false;

} else

return true;

}

In my case, the elements of the form were simply fields, like



'loginForm:changepwdbtn’ – is the id of the button to be clicked when user hits ‘Enter’.

As shown above, call submitEnter() on ‘onkeypress’ of all the elements where you want to use Enter button to perform some action.

Same logic applies to click a command link on Enter, only change is in the submitEnter method.

Instead of

document.getElementById(commandId).click();

use

clickLink(commandId);


where definition of clickLink() is as below:

function clickLink(linkId) {

var fireOnThis = document.getElementById(linkId)

if (document.createEvent) {

var evObj = document.createEvent('MouseEvents')

evObj.initEvent( 'click', true, false )

fireOnThis.dispatchEvent(evObj)

} else if (document.createEventObject) {

fireOnThis.fireEvent('onclick')

}

}

P.S: What I observed (may not be true for all) is that, first in the first form used in a page gets associated with Enter button by default. No special logic/method as explained above is required for such cases. But when you want Enter button to click any button other than the first one, then this post might be useful

And also http://wiki.apache.org/myfaces/SubmitPageOnValueChange should be very useful.


Hope this helps!

05 December 2009

get Internet Explorer version through javascript

Effective way to get Internet Explorer version through Javascript is

function getIEVersion() {
// Returns the version of Internet Explorer or a -1
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}

Hope this helps!

11 August 2009

Work...work...work...

Oh! I just forgot this space...really was very busy killing time consistently at my office with jsf.

Work...work...work...

19 January 2009

The value of attribute "..." associated with an element type "..." must not contain the '<' character.

A parameter was declared under the xsl as


I was trying to populate an attribute of a node in xsl, by using the above declared parameter value as


But this yielded the following error.

The value of attribute "attrName" associated with an element type "parentnode" must not contain the '<' character.


After trying lot of combinations/ways, I found out a way to do it and is

That is simply access the xsl:param as you access a xsl:variable.

Another way of doing it is first declare a variable whose value is the xsl:param value, then use this variable in the same way as did it in the above case


As you can see the second approach is not recommended over the first one.

Hope this helps!

03 October 2008

reading File to String in Java

There are lots of ways to read the contents of File to String in Java. Here I present one of the simplest way to do the job.
FileInputStream fis = new FileInputStream(new File(“C:\\fileName.txt”));
int avail = fis.available();
byte[] bytes = new byte[avail];
fis.read(bytes, 0, avail);
String rfResponse = new String(bytes);
The above code doesn’t involve any looping over the file line by line, as it picks up whole content at once and assigns it to String.

better way of comparing Strings

Lets say there is some String variable, to be compared with some static value in the program,
what generally developers do is to explicitly check on 'nulls' and use equalsIgnoreCase() to compare with that static String.
For example, consider varString is of type String and is to be compared with the static value "Success". Generally to check whether the varString is 'Success' or not, I see people coding it like
if(varString != null && varString.equalsIgnoreCase("Success")) ...
but there is another better way of doing this, that is simply write
if("Success".equalsIgnoreCase(varString)) ...
the above statement includes the check on nulls and the String value at once. No one claims there could be performance uplift due to this statement, but code looks credible and simple.
Just try....