Unexpected <jsp:setProperty> Behavior
The
<jsp:setProperty>
action can be used to
automatically set all properties in a bean with names matching the
names of the
parameters received with
the request. This is a great feature that’s used in many of the
examples in this book. But unless you know how this works behind the
scenes, you could be in for a surprise.
When the <jsp:setProperty>
code is invoked,
it gets a list of all request parameter names and uses bean
introspection to find the corresponding property setter methods. It
then calls all setter methods to set the properties to the values of
the parameters. This means that if you have a property in your bean
that doesn’t match a parameter, the setter method for this
property is not called. In most cases, this is not surprising.
However, if the parameter is present in some requests but not in
others, things may get a bit confusing. This is the case with
parameters corresponding to checkbox, radio button, and selection
list elements in an HTML form. If this type of element is selected,
the browser sends a parameter with the element’s name and the
value of the selected item. But if the element is not selected, it
doesn’t send a parameter at all.
For example, let’s say you have a bean with an indexed
property, such as the projects
property in the
com.ora.jsp.beans.emp.EmployeeBean
used in Chapter 10. This bean is kept in the session scope. The user can change the value of the property through a group of checkboxes in a form. To ...
Get Java Server Pages now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.