Monthly Archives: June 2009

a4j:commandLink action not working in rich:datatable

I was trying to put <a4j:commandLink>  in <rich:datatable> but it was not working. None of its action or actionListener were getting called. After wasting a lot of time and trying a lot I found that the value{Collection} used for dataTable has to be in session scoped bean. If it is in request scoped bean then the action is not getting executed.

Owing to some performance issues I was not willing to put the bean in session so I splited the bean into two parts: one having the session related properties and other were having all the properties whose states were not needed to be maintained.

Looks like this is an error in the implementation. Also <h:comandLink> doesnt work in <rich:datatable>. The reason is unknown till date. Hope we will find it soon.

According to Tim Holloway of Javaranch:  Request-scope beans have very short lived. They only exist for a single request – for example, the one that presented the datatable.

Unfortunately, when you are attempting to select a row in a datatable for further processing, that requires a second request. Since the original request was destroyed, a new request bean gets constructed. But it doesn’t have the row context that the previous request did. That’s why you need a session-scope object. So it will survive over multiple requests and maintain the datamodel and current row indicators.


Posted by on June 22, 2009 in JSF


Simple Human readable time in Java

Want to show time like shown in Twitter or Orkut about an event. Want to identify the time of event in weeks, days or hours as compared to the given time. If yes then Pretty Time provides solution for this. Being open source it will not have any additional cost and yes the learning curve is also very small.

PrettyTime is an OpenSource time formatting library. Completely customizable, PrettyTime creates human readable, relative timestamps like those seen on Digg, Twitter, and Facebook. It’s simple, get started “right now!”

Downlaod Pretty Time API from

Using it is very simple.

The following code will help you understand how to use it.

import java.util.Date;
import com.ocpsoft.pretty.time.PrettyTime;

public class BasicJavaApp
	public static void main(String[] args)
		PrettyTime p = new PrettyTime();
		System.out.println(p.format(new Date()));

Will give you the output “right now”

Leave a comment

Posted by on June 20, 2009 in Java


Java Strings

Whenever we make a String object by doing some operation on the Strings then we will always end up with new String Object.

String class has overridden hashCode() and equals() method so meaningfully the String objects may be equal and they will return the same hash code but then also they are situated at different memory location in the Objcet heap.

The default implementation of hashCode() provided by Object is derived by mapping the memory address of the object to an integer value. Because on some architectures the address space is larger than the range of values for int, it is possible that two distinct objects could have the same hashCode(). If you override hashCode(), you can still use the System.identityHashCode() method to access this default value.

The following example will simplify my point.

public class MWC109 {
public static void main(String args[]) {
String a = "A", b = "B", c = a+b, d = a+b;
System.out.println("a+b hashcode: " + (a+b).hashCode());
System.out.println("c hashcode: " +  c.hashCode() );
System.out.println("d hashcode: " + d.hashCode());
System.out.println("c system hash code: " + System.identityHashCode(c) );
System.out.println("d system hash code: " + System.identityHashCode(d) );
for (int i =0; i<10; i++){
System.out.println(i + ") a+b System HashCode: " + System.identityHashCode((a+b)));
String str1 = "POps";
String str2 = "POps";
System.out.println("str1 System HashCode: " +  System.identityHashCode(str1));
System.out.println("str2 System HashCode: " + System.identityHashCode(str2));
System.out.println("str1 hashcode: " + str1.hashCode());
System.out.println("str2 hashcode: " + str2.hashCode());
for (int i=0; i<10; i++){
System.out.println(i + ") str1+str2 System HashCode: " + System.identityHashCode(str1+str2));
System.out.print(((a+b)==(a+b)) + ",");
System.out.print((c==d) + ",");

The output of the above code is

a+b hashcode: 2081
c hashcode: 2081
d hashcode: 2081
c system hash code: 4072869
d system hash code: 1671711
0) a+b System HashCode: 11394033
1) a+b System HashCode: 4384790
2) a+b System HashCode: 9634993
3) a+b System HashCode: 1641745
4) a+b System HashCode: 11077203
5) a+b System HashCode: 14576877
6) a+b System HashCode: 12677476
7) a+b System HashCode: 33263331
8) a+b System HashCode: 6413875
9) a+b System HashCode: 21174459
str1 System HashCode: 827574
str2 System HashCode: 827574
str1 hashcode: 2462786
str2 hashcode: 2462786
0) str1+str2 System HashCode: 17510567
1) str1+str2 System HashCode: 27744459
2) str1+str2 System HashCode: 28737396
3) str1+str2 System HashCode: 6927154
4) str1+str2 System HashCode: 24355087
5) str1+str2 System HashCode: 5442986
6) str1+str2 System HashCode: 10891203
7) str1+str2 System HashCode: 9023134
8) str1+str2 System HashCode: 19336051
9) str1+str2 System HashCode: 6336176

So it is visible that whenever we make some String object by doing some alteration we will always end up in a new Object in heap. So the system HashCode will be different for them as no two Objects can share the same memory.

The hashcodes we get from the String Objects return the hashcode generated by this function as it is overridden in String class.

public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
hash = h;
return h;

To get the actaul hashcode we used the static function identityHashCode() of System class.
The system hashcode for str1 and str2 are same as those objects were created without any alteration on any of the objects.v

1 Comment

Posted by on June 12, 2009 in Java



Making comparators on the fly using Apache BeanComparator

We can use the BeanComparator instead of writing new comparators for each and every fields.

//sort by country name
BeanComparator comp = new BeanComparator(“”);
Collections.sort(list, comp);

It will sort the same collection.

Leave a comment

Posted by on June 5, 2009 in Java