Bean tags convert primitive properties automatically

If you’re familiar with JavaBeans from any earlier lifetime, this is no surprise to you. JavaBean properties can be anything, but if they’re Strings or primitives, all the coercing is done for you.

That’s right—you don’t have to do the parsing and conversion yourself

If we make the type Employee (instead of Person)

image with no caption

It all works

image with no caption

The <jsp:setProperty> action takes the String request parameter, converts it to an int, and passes that int to the bean’s setter method for that property.

Watch it!

Automatic String-to-primitive conversion does NOT work if you use scripting!! It fails even if an expression is INSIDE the <jsp:setProperty> tag.

If you use the <jsp:setProperty> standard action tag with the property wildcard, OR just a property name without a value or param attribute (which means the property name matches the request parameter name), OR you use a param attribute to indicate the request parameter whose value should be assigned to the bean’s property, OR you type a literal value, the automatic conversion from String to int works. Each of these examples converts automatically:

image with no caption

BUT... if you use scripting, the automatic conversion does NOT work:

image with no caption
image with no caption

Note

The bean standard action tags are more natural to a non-programmer.

Once again, the benefit of using tags over scripting is more about the web page designers than about you (the Java programmer). Although even Java programmers find that tags are easier to maintain than hard-coded Java scripting elements. With the bean-related tags, the designer needs only the basic identification info (attribute name, scope, and property name). True, they do have to know the fully-qualified class name, but as far as the web page designer knows—it’s just a name with dots (.) in it. The web designer doesn’t need any knowledge of what’s really behind it, and they can think of beans as simply records with fields. You tell the designers the record (the class and the identifier) and the fields (the properties).

Still, the bean standard actions aren’t as elegant as they could be.

And that’s why this isn’t the end of the story on scriptless pages. Read on...

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset