On Wed, 22 Jan 2003 [EMAIL PROTECTED] wrote:
> Date: Wed, 22 Jan 2003 14:26:08 -0600 > From: [EMAIL PROTECTED] > Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]> > To: [EMAIL PROTECTED] > Subject: null references in ActionForms > > Hi all, > > I'm curious about why forms don't like null references. Let's say I have a > form with a ref to a domain class that I wrote. That class has a name > attribute. Struts complains when it tries to reference > someForm.someClass.name if someClass is null. In the case of displaying an > entry page for the first time, if someForm.someClass is null, Struts I think > should either ignore it, as it would if someForm.someClass.name were null, > or it should call the default constructor and go from there. Why does it > not do this? Can it be configured to do so? Is there an alternative? I > see FormPropertyConfig but it only applies to DynaForms. For now I've > tweaked my getters to return a new instance if the ref is null, but I'm > uncomfortable with this. > There are lots of opinions on how evaluation of something like this should happen :-). JSTL, for example, chose a path somewhat more like what you suggest -- it returns a zero-length string for situations like this. For the target audience of JSTL (page authors), that probably makes sense, because the ultimate destination is usually the output page where you need Strings. It doesn't make *quite* as much sense for general purpose property manipulation in a Java program. In order to do something "smarter" than that, though, you've got to figure out how to instantiate the "someClass" variable. A default constructor might work, IF there is one, and IF you know what class you're talking about (you won't if someForm is actually a Map). And, for some people, the preference would be to configure some sort of a factory for such objects. And then you have to configure the mess ... In practical terms, it's too late in a 1.1 time frame to change this, and it would undoubtedly cause backwards compatibility problems as well, so implementing a change this radical in any 1.x release seems problematic. My personal preference is going to be to encourage folks to move towards using JSTL expressions (which are also getting built in to JSP 2.0 so you can use them everywhere in your page). As a transition tool, the struts-el libraries are included in 1.1 (in the "contrib" directory) -- they are variations on existing Struts tags, but accept the standard EL syntax (and therefore behave the way that JSTL tags do with respect to null values like this). > thanks > > john > Craig McClanahan -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>