July 21, 2004: These little nagging programming problems

Sometimes, often, while programming you encounter these little nagging problems. You type something as you've always did but somehow it's not working as expected or worse, it's not working at all. Sometimes it's a typo but at other times the context is slightly different. These problems can sometimes take a long time to solve or even drive you to a fast workaround. Fortunately the internet offers a lot of possibilities for help, but how hard it can be to find. I'll discuss a few examples from my own experience.
In my projects I use Struts a lot. It takes some time to master and also some discipline but I'm quite happy with it. Today I experienced two of these little nagging probles:
1 Tag attributes, jsp expressions and javascript
It took me a while to discover that it does not seem possible to use a struts tag with an event attribute to call a javascript function with a parameter and this parameter was to be taken (as a static) from some java class class. Like this:
<html:select onClick="somefunction('<%=SomeClass.ACONSTANT%>')"/>
Fortunately I had a simple workaround, by using specialized javascript functions, but still it's annoying.
Any suggestions are welcome.
2 ActionForm properties and the HTML form select elements
On my form I have two select, with option elements displayed as listboxes (size=n). Upon a click in one of the boxes it triggers some javascripts that deselects the other. After a submit the value of the selected box is 'transferred' to the, session scoped, actionform. After processing, the same page is generated by a jsp and the same item in the same listbox (let's call it A) will be selected. When I now select the other listbox (B), A will be deselected. And now I submit again, Io some processing and load the same page again. And as you'd probably guessed already (but I did not) both listboxes have been selected. Since I use a session scoped action form, the form is kept in the session and with it the populated properties. After a submit, the population of the actionform does not seem to involve the empty select element (I didn't test other elements) and therefore the corresponding property will keep ist's value. This result in a form that is an incorrect representation of the page.
The workaround for this is a lot more trickier than the former problem. It can involve a dummy option in the listbox. It can involve some kind of hidden shadow html elements. It can involve some trickery on the setProperty methods on the actionform. It can be handled with an extra hidden element that identifies which listbox has been selected. Probably other options are available too. Be aware that setting a listbox to a value that is not one of its options it is effectually deselecting it (with a value of null, selectedIndex=-1).
3 Not resetting a session scoped ActionForm
Today we experienced a weird problem concerning the resetting of a session scoped Action form. I removed the, empty, reset (ActionMapping mapping, HttpServletRequest request) method in my actionForm and didn't expect much to happen. How foolish of me... :-(( Instead, all the form properties were cleared and the form was populated only with the html form elements. Because this was part of multiple changes we didn't identify the removal of the reset method to be the culprit and it took some time to figure it out. Therefore: implement an empty reset (..,..) method to prevent the session scoped ActionForm to be resetted.
Posted by Aino at 21:26