Saturday, March 1, 2008

Struts Multi-Select Form Wipes out Data

I had a multiple select box in a form into which users could add/remove some attributes. On submitting the system was supposed to update the database with additions/removals done by the user.

But every time i submitted the form, all the records in the specific table was getting deleted. After spending a considerable amount of time scratching my head, i tried to set the focus on that field prior to submitting the form and then it worked.

var options = document.getElementById("selectedKeys").options;
for (var i = 0; i < options.length; i++) {
options[i].selected = true;
}
form.submit();

I am not sure if it is anything to do with Struts forms, or HTML in general.

Setting css property in JavaScript calls

I wanted to dynamically set color for some of the options in a drop down list. I tried the following call:
document.getElementById("selectedKeys").options[i].style = 'color:red';

This did not work, at least in IE.

For this to work, i had to do something like this:
document.getElementById("selectedKeys").options[i].style.color = 'red';

SimpleDateFormat is lenient

Today i discovered something about SimpleDateFormat, a Java Date Formatter API.

I used a statement like,
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

In my unit test i wanted a failing test when i pass in a String "13/01/2008". The method
df.parse()
has a ParseException in its throws clause. I was hoping that, that exception will be thrown when this invalid date was passed in..

Instead it parsed it as January 1 2009.

When i did some digging around, i realised that there was a method called setLenient.

Once i made a call df.setLenient(false), prior to the parse call, it threw the ParseException when invalid date was passed in..

I found it a little weird and would have expected setLenient to be set to false by default.