Archive for the ‘Programming’ Category

Set Event Handlers For Row Elements With Prototype

Sunday, January 10th, 2010

This article describes how to set event handlers for row elements using Prototype. This is especially handy when dynamically creating new elements on a page.

In this example, we have a data grid displayed as a table element. The id of the table is ‘myTableId’. We are going to set up two event handlers for each table row. On a single mouse click event, we’ll call a function that highlights the selected row. On a double click, we’ll call a function that opens the row data for editing. Both of these functions are omitted for brevity.

function setEventHandlers() {
    //get all row elements under element with id ‘myTableId’
    var rows = $$('#myTableId tr');
    rows.each(function(rowElement) {
        rowElement.observe('click', highlightRow);
        rowElement.observe('dblclick', editRow);
    });
}

Once again, Prototype does the dirty work of handling the differences in event handling between browsers. All we have to do is attach an event handler function to an element using the observe() method, and Prototype handles the rest.

Check out the details on the options available on the Event object in Prototype.

JBoss JNDI datasource not releasing connections with Spring/Hibernate

Saturday, October 24th, 2009

I recently had a problem with my application not releasing the database connections when I switched to using JNDI. This was a JEE application that used Spring and Hibernate. Here are the versions that I was using:

  • JBoss 4.0.4
  • JDK 1.5
  • Spring 2.5.5
  • Hibernate 3.2.6

Solution

In my Spring applicationContext.xml file, I added the datasource JNDI entry for “Kramerica”. I referenced it in the Hibernate session factory bean:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="java:comp/env/jdbc/Kramerica"/>
</bean>
 
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  <property name="dataSource" ref="dataSource"/>
  <property name="hibernateProperties">
    <props>
	<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
	<prop key="hibernate.show_sql">false</prop>
	<prop key="hibernate.connection.release_mode">after_transaction</prop>
    </props>
  </property>
</bean>

The key entry is the property “hibernate.connection.release_mode”.
This property specifies when Hibernate should release JDBC connections.

By default, a JDBC connection is held until the session is explicitly closed or disconnected. For an application server JTA datasource, use “after_statement” to aggressively release connections after every JDBC call. For a non-JTA connection, it often makes sense to release the connection at the end of each transaction, by using “after_transaction”.

You can also set it to “auto”, which will choose “after_statement” for the JTA and CMT transaction strategies and “after_transaction” for the JDBC transaction strategy.

You can find more detail on the Hibernate connection settings at the Hibernate site. Happy coding!

Iterate over a Map using Java 5 generics

Sunday, August 2nd, 2009

One of the coolest features in Java 5 is the enhanced for loop. Instead of having to use clunky Iterators to loop through a Map, you can use the enhanced for loop just as though you were iterating over an array, a List, or other collection. (more…)

JavaScript validation of dynamically added form fields

Sunday, July 5th, 2009

This article describes how to use JavaScript and the Prototype library to validate form fields that can be dynamically added at runtime. For example, let’s say that we have a table and the user can insert one or many new rows. Each row might contain multiple input text fields, which must be validated upon form submission.
(more…)

Managing legacy user login object with Spring (Part 2)

Friday, June 26th, 2009

In part 1 of this article, we discussed some alternatives to handling a user login object that is controlled by a legacy application framework outside of Spring. We also discussed some disadvantages with our approach. (more…)

Using Hibernate Validator for your Java classes

Friday, June 12th, 2009

This article discusses how to use the Hibernate Validator for your Java classes. (more…)

Managing legacy user login object with Spring (Part 1)

Sunday, June 7th, 2009

Using Spring to manage your Java beans for dependency injection is fairly straightforward, but what do you do when you want Spring to manage beans that were created outside of the Spring container? I encountered this issue on a recent project. The user login credentials were handled by a legacy system and stored in the HTTP session. (more…)

ClassNotFound errors - Hibernate 3.1 and Spring 1.2

Sunday, June 7th, 2009

I recently had a project that incorporated Hibernate and Spring. MyEclipse 5.0 was the IDE, and the app server was JBoss 4.0.4. We encountered a few problems trying to get the correct combination of Hibernate, Spring, and JBoss that would play well together. (more…)

Sorting data in Java using Apache BeanUtils

Thursday, May 21st, 2009

Most data driven applications have a requirement to be able to sort data. Most of the time this can be accomplished at the data access layer by sorting the data when it is retrieved via SQL query, or by using JavaScript or some other scripting language to sort the data in the view layer. However, there are also times when it is convenient to sort a collection of POJOs in the business layer. (more…)

Cross browser issues with utility functions in Prototype

Thursday, May 7th, 2009

When using utility functions in Prototype library (e.g., F$), the element name must be surrounded by quotes. Firefox requires it to not include quotes, but IE does not.

This example does not work:

$F(sample);  //DOES NOT WORK!!

This example does work:

$F(‘sample’);  //DOES WORK!

This was tested with Firefox 3 and IE7 using Prototype 1.5.1.