Archive for the 'Java' Category

Message Handling with JSF and Redirects

Sunday, March 1st, 2009

The JavaServer Faces-Framework offers a mechanism to present messages to the user. The meesages can be created by the framework itself, for example during the conversion oder validation phase. But it is also possible to create custom messages within your actual application.

But there is a problem, if you use redirects instead of forwards, e.g. by using the <redirect/> tag in faces.config.xml. A redirect leads to a second request loading the following page. The messages are only valid in the current request and are thus not displayed on the following page.
(more…)

Determine current method in Java

Friday, February 13th, 2009

A rather strange way to determine the method, where you currently are, but it works:

1
2
3
4
5
6
7
8
9
private String getCurrentMethodName() {
   List stackTraceList = Arrays.asList((new Throwable()).getStackTrace());
   for (Iterator it=stackTraceList.iterator();it.hasNext();) {
      StackTraceElement ste = (StackTraceElement) it.next();
      if (!ste.getClassName().equals(this.getClass().getName())) 
         return ste.getClassName().concat(".").concat(ste.getMethodName());
   }
   return null;
}

I implemented it the way that the first method from the stacktrace is returned that does not belong to the class, where this code resides. So it is possible to use this code for example in an own logging class to augment the logging messages with the method name.

You can find another example here: http://dev.kanngard.net/