Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Strut your stuff with JSP tags

Use and extend the open source Struts JSP tag library

  • Print
  • Feedback

Page 2 of 5

Automatic properties function only within constructs that belong to Struts. Thus the getProperty and setProperty tags can't see them. Instead, you must use JSP expressions (for getProperty) and statements (for setProperty). In particular, instead of using the setProperty tag with property="*" to map request parameters to properties by name, you call Struts's populate function to do it.

To specify the functionality that an automatic bean must provide, let's jot down the interface shown in Listing 2. In short, an AutoBean interface implementation must be able to associate a value with a property name. Being able to get and set objects, and not only strings, is convenient because some Struts tags require collections as attributes such as the options tag for the days in the example above.

Listing 2. AutoBean.java: The AutoBean interface

package com.bank;
public interface AutoBean {
    public void set(String property, Object value);
    public String get(String property);
    public Object getObject(String property);
}


To make Struts work with automatic properties, you must perform some minor surgery on the BeanUtils class. The change is nondestructive in that it makes no difference for beans that don't implement the AutoBean interface. Essentially, you implant code to get your property if the bean has no real property with the given name. Similarly, you implant your code in the populate function to use your set function when there is no real property setter for a given name. Listing 3 shows those changes:

Listing 3. BeanUtils.java: Implanting AutoBeans functionality in Struts

// new function for autobeanspublic static Object getAutoPropertyValue(Object bean, String name)
throws Exception {
    if(bean instanceof com.bank.AutoBean) 
        return ((com.bank.AutoBean) bean).getObject(name);
    throw new NoSuchMethodException("Unknown property '" + name + "'");
} 
public static Object getPropertyValue(Object bean, String name)
throws Exception {
    // Retrieve the property getter method for the specified property
    PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);
    if (descriptor == null)       // throw new NoSuchMethodException("Unknown property '" + name + "'");       return getAutoPropertyValue(bean, name);
  ...
public static void populate(Object bean, Hashtable properties)
throws Exception {
    ...
    if (setter == null) {       if(bean instanceof com.bank.AutoBean)
            ((com.bank.AutoBean) bean).set(name, value); 
        continue;
    }
    ...


An implementation of the AutoBean interface is presented in the last section of this article. Let's now turn to the issue of validating user input.

Validating user input with regular expressions

One of the mindless chores facing the JSP programmer is the writing and maintenance of trivial user input validation. Why not use the power of regular expressions to deal with that? And why not specify that right in the tag instead of having to write customized code around each and every input? That will eliminate the great majority of validation chores. For some tricky inputs, particularly when the validity depends on a combination of fields, you still must write validation code.

  • Print
  • Feedback

Resources