Monday, September 24, 2007

Hackers and Painters

Something I have always believed in, but never had the right words to express. Brilliant description of the creativity and artistry involved in the making of software: Hackers and Painters

Labels:

Thursday, September 20, 2007

Surprising tip

I was taken aback by the tip A Very Simple Way to Skip Part of Code Without Using Comment Symbols duly linked from http://java.sun.com. It is a surprise way of inserting return statements in the middle of the code to test only the functionality upto the point of return.

Commenting out code and testing, or creating appropriate test harnesses so that all cases can be covered is surprisingly not advocated. Nor is the need for modular methods and further breakdown of code logically.

What is next, Goto ?

Labels: ,

Tuesday, September 18, 2007

Sortable table using JSF and AJAX4JSF

I have written about AJAX and JSF in my previous post. Let us now look at a way to implement sortable tables using JSF and AJAX. We want to build a table which has the header as links. If a particular link is clicked, the table entries are sorted based on the header clicked. We want the entire functionality to be AJAX based, so that only part of the page is refreshed.

A normal JSF table looks like this:



<h:datatable id="addressTableHeader" border="0" cellpadding="0" cellspacing="0">
<h:column>
<f:facet name="header">
<h:outputtext value="Post Code">
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputtext value="Street">
</f:facet>
</h:column>
</h:dataTable>
<h:datatable id="addressTableBody" var="tableData" value="#{Bean.addressList}">
<h:column>
<h:outputtext value="#{tabledata.postcode}">
</h:column>
<h:column>
<h:outputtext value="#{tabledata.street}">
</h:column>
</h:dataTable>





The backing bean Bean.java has a method
getAddressList()
which wraps an ArrayList inside a ListDataModel and returns it.

We modify the table header from plain text to links which fire AJAX requests, using Ajax4JSF. We want the table body to be refreshed when the header is pressed, so we specify that in the reRender property of the commandLink.


<h:datatable id="addressTableHeader" border="0" cellpadding="0" cellspacing="0">
<h:column>
<f:facet name="header">
<a4j:commandlink action="#{Bean.postcodeSort}" ajaxsingle="true" rerender="addressTableBody" immediate="true">
<h:outputtext value="Post Code" styleclass="tariffpopupsearchheading">
</a4j:commandLink>
</f:facet>

</h:column>
<h:column>
<f:facet name="header">
<a4j:commandlink action="#{Bean.streetSort}" ajaxsingle="true" rerender="addressTableBody" immediate="true">
<h:outputtext value="Street" styleclass="tariffpopupsearchheading">
</a4j:commandLink>
</f:facet>
</h:column>
</h:dataTable>


We now add a Comparator to the back end code, which can help to sort the ArrayList of data to be presented in the table. The Comparator has different sorting criteria. New methods
postcodeSort()
and
streetSort()
are added to the Bean, which help to set the sorting criteria on click . The Bean's
getAddresslist()
method is modified to pass the appropriate Comparator.

Labels: , , ,

Friday, September 14, 2007

How to Handle Java Finalization's Memory-Retention Issues

Interesting article on the pitfalls of Java finalization : How to Handle Java Finalization's Memory-Retention Issues. Wonder whether finalization really makes sense.

Labels:

Wednesday, September 12, 2007

JSF and AJAX

The Ajax4jsf Framework is a very interesting and fruitful way of adding AJAX support to the JSF applications. I do not know much AJAX, but I was able to add AJAX support to my application easily.

An interesting link for re-rendering input text boxes. can be found here. I struggled a lot with this concept, but the link helped make things very clear.

Labels: , ,