org.springframework.web.servlet.tags.form
Class OptionWriter

java.lang.Object
  extended by org.springframework.web.servlet.tags.form.OptionWriter
Direct Known Subclasses:
OptionsTag.OptionsWriter

 class OptionWriter
extends java.lang.Object

Provides supporting functionality to render a list of 'option' tags based on some source object. This object can be either an array, a Collection, or a Map.

Using an array or a Collection:

If you supply an array or Collection source object to render the inner 'option' tags, you may optionally specify the name of the property on the objects which corresponds to the value of the rendered 'option' (i.e., the valueProperty) and the name of the property that corresponds to the label (i.e., the labelProperty). These properties are then used when rendering each element of the array/Collection as an 'option'. If either property name is omitted, the value of Object.toString() of the corresponding array/Collection element is used instead. However, if the item is an enum, Enum.name() is used as the default value.

Using a Map:

You can alternatively choose to render 'option' tags by supplying a Map as the source object.

If you omit property names for the value and label:

If you supply property names for the value and label:

When using either of these approaches:

Since:
2.0
Author:
Rob Harrop, Juergen Hoeller, Sam Brannen, Scott Andrews

Field Summary
private  BindStatus bindStatus
           
private  boolean htmlEscape
           
private  java.lang.String labelProperty
           
private  java.lang.Object optionSource
           
private  java.lang.String valueProperty
           
 
Constructor Summary
OptionWriter(java.lang.Object optionSource, BindStatus bindStatus, java.lang.String valueProperty, java.lang.String labelProperty, boolean htmlEscape)
          Creates a new OptionWriter for the supplied objectSource.
 
Method Summary
private  void doRenderFromCollection(java.util.Collection optionCollection, TagWriter tagWriter)
          Renders the inner 'option' tags using the supplied Collection of objects as the source.
private  java.lang.String getDisplayString(java.lang.Object value)
          Determines the display value of the supplied Object, HTML-escaped as required.
protected  boolean isOptionDisabled()
          Determine whether the option fields should be disabled.
private  boolean isOptionSelected(java.lang.Object resolvedValue)
          Determine whether the supplied values matched the selected value.
protected  java.lang.String processOptionValue(java.lang.String resolvedValue)
          Process the option value before it is written.
private  void renderFromArray(TagWriter tagWriter)
          Renders the inner 'option' tags using the optionSource.
private  void renderFromCollection(TagWriter tagWriter)
          Renders the inner 'option' tags using the optionSource.
private  void renderFromEnum(TagWriter tagWriter)
          Renders the inner 'option' tags using the optionSource.
private  void renderFromMap(TagWriter tagWriter)
          Renders the inner 'option' tags using the supplied Map as the source.
private  void renderOption(TagWriter tagWriter, java.lang.Object item, java.lang.Object value, java.lang.Object label)
          Renders an HTML 'option' with the supplied value and label.
protected  void writeCommonAttributes(TagWriter tagWriter)
          Writes default attributes configured to the supplied TagWriter.
 void writeOptions(TagWriter tagWriter)
          Write the 'option' tags for the configured optionSource to the supplied TagWriter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

optionSource

private final java.lang.Object optionSource

bindStatus

private final BindStatus bindStatus

valueProperty

private final java.lang.String valueProperty

labelProperty

private final java.lang.String labelProperty

htmlEscape

private final boolean htmlEscape
Constructor Detail

OptionWriter

public OptionWriter(java.lang.Object optionSource,
                    BindStatus bindStatus,
                    java.lang.String valueProperty,
                    java.lang.String labelProperty,
                    boolean htmlEscape)
Creates a new OptionWriter for the supplied objectSource.

Parameters:
optionSource - the source of the options (never null)
bindStatus - the BindStatus for the bound value (never null)
valueProperty - the name of the property used to render option values (optional)
labelProperty - the name of the property used to render option labels (optional)
Method Detail

writeOptions

public void writeOptions(TagWriter tagWriter)
                  throws JspException
Write the 'option' tags for the configured optionSource to the supplied TagWriter.

Throws:
JspException

renderFromArray

private void renderFromArray(TagWriter tagWriter)
                      throws JspException
Renders the inner 'option' tags using the optionSource.

Throws:
JspException
See Also:
doRenderFromCollection(java.util.Collection, TagWriter)

renderFromMap

private void renderFromMap(TagWriter tagWriter)
                    throws JspException
Renders the inner 'option' tags using the supplied Map as the source.

Throws:
JspException
See Also:
renderOption(TagWriter, Object, Object, Object)

renderFromCollection

private void renderFromCollection(TagWriter tagWriter)
                           throws JspException
Renders the inner 'option' tags using the optionSource.

Throws:
JspException
See Also:
doRenderFromCollection(java.util.Collection, TagWriter)

renderFromEnum

private void renderFromEnum(TagWriter tagWriter)
                     throws JspException
Renders the inner 'option' tags using the optionSource.

Throws:
JspException
See Also:
doRenderFromCollection(java.util.Collection, TagWriter)

doRenderFromCollection

private void doRenderFromCollection(java.util.Collection optionCollection,
                                    TagWriter tagWriter)
                             throws JspException
Renders the inner 'option' tags using the supplied Collection of objects as the source. The value of the valueProperty field is used when rendering the 'value' of the 'option' and the value of the labelProperty property is used when rendering the label.

Throws:
JspException

renderOption

private void renderOption(TagWriter tagWriter,
                          java.lang.Object item,
                          java.lang.Object value,
                          java.lang.Object label)
                   throws JspException
Renders an HTML 'option' with the supplied value and label. Marks the value as 'selected' if either the item itself or its value match the bound value.

Throws:
JspException

getDisplayString

private java.lang.String getDisplayString(java.lang.Object value)
Determines the display value of the supplied Object, HTML-escaped as required.


processOptionValue

protected java.lang.String processOptionValue(java.lang.String resolvedValue)
Process the option value before it is written. The default implementation simply returns the same value unchanged.


isOptionSelected

private boolean isOptionSelected(java.lang.Object resolvedValue)
Determine whether the supplied values matched the selected value. Delegates to SelectedValueComparator.isSelected(org.springframework.web.servlet.support.BindStatus, java.lang.Object).


isOptionDisabled

protected boolean isOptionDisabled()
                            throws JspException
Determine whether the option fields should be disabled.

Throws:
JspException

writeCommonAttributes

protected void writeCommonAttributes(TagWriter tagWriter)
                              throws JspException
Writes default attributes configured to the supplied TagWriter.

Throws:
JspException