Wednesday, June 22, 2011

Tuesday, April 18, 2006

Setting focus on input fields with JSF and Facelets

JavaServer(tm) Faces components have a dynamic id. This can create problems when trying to access these components from JavaScript code on the client. I my case I wanted to do a very simple and common thing, set input focus on a specific field. One solution is to use a custom component called IDProxy (http://www.jsftutorials.net/proxyTag.html). This component makes it easy to obtain the dynamic generated id for it's parent element. While I found lots of articles on Internet explaining the use of IDProxy with JSF, I found no article that used Facelets as the rendering technique. Facelets (https://facelets.dev.java.net/) is a powerful templating system that allows you to define JSF views using HTML-style templates. In this article I will explain how I used IDProxy to set focus on an input field rendered with Facelets.

Download j4j.jar and put it in the WEB-INF/lib folder of your web project. Create a file called j4j.taglib.xml in the META-INF folder of your web project. The file should look like this:

<?xml version="1.0"?>

<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">

<facelet-taglib>
<namespace>http://javascript4jsf.dev.java.net/</namespace>
<tag>
  <tag-name>idProxy</tag-name>
  <component>
    <component-type>org.j4j.idProxy</component-type>
  </component>
</tag>
</facelet-taglib>

Add the j4j namespace to the xhtml page where you want to use idProxy.

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:j4j="http://javascript4jsf.dev.java.net/">

Add an idProxy tag inside the input element that you want to get the dynamic id from. In my case I want to get the dynamic id of a field called username in a login dialog.

<h:outputlabel for="username">Username</h:outputLabel>
<h:inputtext id="username" value="#{user.username}">
<j4j:idproxy id="_focus"/>
</h:inputText>

Now you can extract the dynamic id through the idProxy element from JavaScript code. The title property of the idProxy element contain the generated id of it's parent element, that's the value we want.

<script language="javascript">
//<![CDATA[
var iid=document.getElementById("_focus");
if(iid != null) {
document.getElementById(iid.title).focus();
}
//]]>
</script>

If you put the JavaScript code in your facelets template, you just have to include the j4j namespace declaration and the idProxy tag in each page where you want to set focus on a field.

This works great for me, but I'm still puzzled... Why is such a common thing like setting focus not standardized in JSF?

Friday, January 20, 2006

Using Quartz job scheduler with Spring

Quartz is an open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application. Quartz can be used to trigger jobs at a specific date and time, at regular intervals, or other very complex schedules.

Although Quartz can be used in a standalone system, this article focuses on using Quartz in a J2EE application built with the Spring framework. The Spring framework has built-in support for Quartz. All you have to do is put the Quartz runtime (quartz-1.x.x.jar) in your classpath, implement the business logic to be triggered, and put some entries in your Spring configuration file.

First you have to configure a scheduler with a list of triggers.

<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
  <list>
    <ref local="reportTrigger"/>
  </list>
</property>
</bean>

Then you configure the managed beans that you want to trigger. In this case, my business logic is implemented as a stateless session bean.

<bean id="mondayMorningReport"
class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
<property name="jndiName"><value>ejb/mondayMorningReport</value></property>
<property name="resourceRef"><value>true</value></property>
<property name="businessInterface">
<value>com.mycompany.Report</value></property>
</bean>

Finally you configure the trigger to fires at a specific time. In my case I want a report to execute every Monday morning at 6.30 am. I use the CronTriggerBean for this purpose.

<bean id="reportTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
  <bean
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject"><ref bean="mondayMorningReport"/></property>
    <property name="targetMethod"><value>printReport</value></property>
  </bean>
</property>
<property name="cronExpression"><value>0 30 6 * * MON</value></property>
</bean>

A "Cron-Expression" is a string comprised of 6 or 7 fields separated by white space. The 6 mandatory and 1 optional fields are as follows:

Field Name Allowed Values Allowed Special Characters
Seconds 0-59 , - * /
Minutes 0-59 , - * /
Hours 0-23 , - * /
Day-of-month 1-31 , - * ? / L W C
Month 1-12 or JAN-DEC , - * /
Day-of-Week 1-7 or SUN-SAT , - * ? / L C #
Year (Optional) empty, 1970-2099 , - * /

The Quartz plumbing code is included in Spring 1.0 and above but you must also download the Quarts distribution and include its jar in your application. As of this writing, I use quartz-1.5.1.jar in my application.

Resources

Wednesday, October 05, 2005

EJB3: @OneToMany and @OrderBy - Set versus List

I noticed that my (Hibernate) EJB3 entity with a @OneToMany collection did not sort correctly even though I had an @OrderBy("sortcolumn") specified on it. Every time I fetched the entity from database, the collection order was randomly different. The reason was that the collection was declared as java.util.Set, and a Set does not guarantee that the order will remain constant over time. The database performed the order by statement, but the Set did not return elements in the same order from time to time. Changing the collection type to java.util.List solved the problem, the sort order remained constant.

Introduction

My name is Göran Ehrsson. I live in Sweden, Scandinavia and was born in the mid 60's. I'm an enterprise systems architect and have been developing enterprise systems since 1988. In the beginning it was C++, but in late 90's focus shifted to Java and J2EE. I started this blog to share knowledge and experience in software development using Java technology. My success in software development are very depending on articles found on Internet. I own a lot of Java books, but for fast answers, the Internet is outstanding. I would never been where I am today without Internet and search engines. Although my professional job as senior (CRM) systems architect is interesting and gives great real world experience, it has not satisfied my personal need for cutting edge research and development. Therefore most of my knowledge has been gathered at home, at night, when the rest of the family are sleeping... This blog is my way of giving back to the development community what I have learned during 20+ years of software development. It will hopefully include articles about J2EE, EJB3, JBoss, Hibernate, Spring, J2ME and Jini.