|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sourceforge.stripes.controller.DefaultActionBeanPropertyBinder
public class DefaultActionBeanPropertyBinder
Implementation of the ActionBeanPropertyBinder interface that uses Stripes' built in property expression support to perform JavaBean property binding. Several additions/enhancements are available above and beyond the standard JavaBean syntax. These include:
Nested Class Summary | |
---|---|
protected static class |
DefaultActionBeanPropertyBinder.Row
An inner class that represents a "row" of form properties that all have the same index so that we can validate all those properties together. |
Constructor Summary | |
---|---|
DefaultActionBeanPropertyBinder()
|
Method Summary | |
---|---|
ValidationErrors |
bind(ActionBean bean,
ActionBeanContext context,
boolean validate)
Loops through the parameters contained in the request and attempts to bind each one to the supplied ActionBean. |
void |
bind(ActionBean bean,
String propertyName,
Object propertyValue)
Attempt to set the named property on the target bean. |
protected void |
bindMissingValuesAsNull(ActionBean bean,
ActionBeanContext context)
Uses a hidden field to determine what (if any) fields were present in the form but did not get submitted to the server. |
protected void |
bindNonNullValue(ActionBean bean,
PropertyExpressionEvaluation propertyEvaluation,
List<Object> valueOrValues,
Class targetType,
Class scalarType)
Internal helper method to bind one or more values to a single property on an ActionBean. |
protected void |
bindNullValue(ActionBean bean,
String property,
Class<?> type)
Internal helper method that determines what to do when no value was supplied for a given form field (but the field was present on the page). |
protected void |
checkSingleRequiredField(String name,
String strippedName,
String[] values,
StripesRequestWrapper req,
ValidationErrors errors)
Checks to see if a single field's set of values are 'present', where that is defined as having one or more values, and where each value is a non-empty String after it has had white space trimmed from each end. |
protected List<Object> |
convert(ActionBean bean,
ParameterName propertyName,
String[] values,
Class<?> declaredType,
Class<?> scalarType,
ValidationMetadata validationInfo,
List<ValidationError> errors)
Converts the String[] of values for a given parameter in the HttpServletRequest into the desired type of Object. |
protected void |
doExpressionValidation(ActionBean bean,
ParameterName name,
List<Object> values,
ValidationMetadata validationInfo,
ValidationErrors errors)
Performs validation of attribute values using a JSP EL expression if one is defined in the @Validate annotation. |
protected void |
doPostConversionValidations(ActionBean bean,
Map<ParameterName,List<Object>> convertedValues,
ValidationErrors errors)
Performs basic post-conversion validations on the properties of the ActionBean after they have been converted to their rich type by the type conversion system. |
protected void |
doPreConversionValidations(ParameterName propertyName,
String[] values,
ValidationMetadata validationInfo,
List<ValidationError> errors)
Performs several basic validations on the String value supplied in the HttpServletRequest, based on information provided in annotations on the ActionBean. |
protected Configuration |
getConfiguration()
Returns the Configuration object that was passed to the init() method. |
protected Collection<String> |
getFieldsPresentInfo(ActionBean bean)
In a lot of cases (and specifically during wizards) the Stripes form field writes out a hidden field containing a set of field names. |
protected SortedMap<ParameterName,String[]> |
getParameters(ActionBean bean)
Converts the map of parameters in the request into a Map of ParameterName to String[]. |
protected void |
handlePropertyBindingError(ActionBean bean,
ParameterName name,
List<Object> values,
Exception e,
ValidationErrors errors)
Invoked whenever an exception is thrown when attempting to bind a property to an ActionBean. |
void |
init(Configuration configuration)
Looks up and caches in a useful form the metadata necessary to perform validations as properties are bound to the bean. |
protected boolean |
isBindingAllowed(PropertyExpressionEvaluation eval)
Checks to see if binding is permitted for the provided expression evaluation. |
protected String[] |
trim(String[] values,
ValidationMetadata meta)
Inspects the given ValidationMetadata object to determine if the given values
should be trimmed. |
protected void |
validateRequiredFields(Map<ParameterName,String[]> parameters,
ActionBean bean,
ValidationErrors errors)
Validates that all required fields have been submitted. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DefaultActionBeanPropertyBinder()
Method Detail |
---|
public void init(Configuration configuration) throws Exception
init
in interface ConfigurableComponent
configuration
- the Configuration object being used by Stripes
Exception
- should be thrown if the component cannot be configured well enough to use.protected Configuration getConfiguration()
public ValidationErrors bind(ActionBean bean, ActionBeanContext context, boolean validate)
Loops through the parameters contained in the request and attempts to bind each one to the supplied ActionBean. Invokes validation for each of the properties on the bean before binding is attempted. Only fields which do not produce validation errors will be bound to the ActionBean.
Individual property binding is delegated to the other interface method, bind(ActionBean, String, Object), in order to allow for easy extension of this class.
bind
in interface ActionBeanPropertyBinder
bean
- the ActionBean whose properties are to be validated and boundcontext
- the ActionBeanContext of the current requestvalidate
- true indicates that validation should be run, false indicates that only type
conversion should occurprotected boolean isBindingAllowed(PropertyExpressionEvaluation eval)
Checks to see if binding is permitted for the provided expression evaluation. Note that the
expression is available through the getExpression()
and the ActionBean is available
through the getBean()
method on the evaluation.
By default checks to ensure that the expression is not attempting to bind into the ActionBeanContext for security reasons.
eval
- the expression evaluation to check for binding permission
protected void handlePropertyBindingError(ActionBean bean, ParameterName name, List<Object> values, Exception e, ValidationErrors errors)
bean
- the ActionBean that was the subject of bindingname
- the ParameterName object for the parameter being boundvalues
- the list of values being bound, potentially null if the error occurred when
binding a null valuee
- the exception raised during bindingerrors
- the validation errors object associated to the ActionBeanprotected void bindMissingValuesAsNull(ActionBean bean, ActionBeanContext context)
bean
- the ActionBean being bound tocontext
- the current ActionBeanContextprotected Collection<String> getFieldsPresentInfo(ActionBean bean)
bean
- the current ActionBean
protected void bindNonNullValue(ActionBean bean, PropertyExpressionEvaluation propertyEvaluation, List<Object> valueOrValues, Class targetType, Class scalarType) throws Exception
bean
- the ActionBean instance to which the property is being boundpropertyEvaluation
- the property evaluation to be used to set the propertyvalueOrValues
- a List containing one or more valuestargetType
- the declared type of the property on the ActionBean
Exception
- if the property cannot be bound for any reasonprotected void bindNullValue(ActionBean bean, String property, Class<?> type) throws ExpressionException
bean
- the ActionBean to which properties are being boundproperty
- the name of the property being boundtype
- the declared type of the property on the ActionBean
ExpressionException
- if the value cannot be manipulated for any reasonprotected SortedMap<ParameterName,String[]> getParameters(ActionBean bean)
public void bind(ActionBean bean, String propertyName, Object propertyValue) throws Exception
bind
in interface ActionBeanPropertyBinder
bean
- the ActionBean on to which the property is to be boundpropertyName
- the name of the property to be bound (simple or complex)propertyValue
- the value of the target property
Exception
- thrown if the property cannot be bound for any reasonprotected void validateRequiredFields(Map<ParameterName,String[]> parameters, ActionBean bean, ValidationErrors errors)
protected void checkSingleRequiredField(String name, String strippedName, String[] values, StripesRequestWrapper req, ValidationErrors errors)
Checks to see if a single field's set of values are 'present', where that is defined as having one or more values, and where each value is a non-empty String after it has had white space trimmed from each end.
For any fields that fail validation, creates a ScopedLocaliableError that uses the stripped name of the field to find localized info (e.g. foo.bar instead of foo[1].bar). The error is bound to the actual field on the form though, e.g. foo[1].bar.
name
- the name of the parameter verbatim from the requeststrippedName
- the name of the parameter with any indexing removed from itvalues
- the String[] of values that was submitted in the requesterrors
- a ValidationErrors object into which errors can be placedprotected void doPreConversionValidations(ParameterName propertyName, String[] values, ValidationMetadata validationInfo, List<ValidationError> errors)
propertyName
- the name of the property being validated (used for constructing errors)values
- the String[] of values from the request being validatedvalidationInfo
- the ValidationMetadata for the property being validatederrors
- a collection of errors to be populated with any validation errors discoveredprotected void doPostConversionValidations(ActionBean bean, Map<ParameterName,List<Object>> convertedValues, ValidationErrors errors)
bean
- the ActionBean that is undergoing validation and bindingconvertedValues
- a map of ParameterName to all converted values for each fielderrors
- the validation errors object to put errors in toprotected void doExpressionValidation(ActionBean bean, ParameterName name, List<Object> values, ValidationMetadata validationInfo, ValidationErrors errors)
ExpressionValidator
for details
on how this is implemented.
bean
- the ActionBean who's property is being validatedname
- the name of the property being validatedvalues
- the non-null post-conversion values for the propertyvalidationInfo
- the validation metadata for the propertyerrors
- the validation errors object to add errors toprotected List<Object> convert(ActionBean bean, ParameterName propertyName, String[] values, Class<?> declaredType, Class<?> scalarType, ValidationMetadata validationInfo, List<ValidationError> errors) throws Exception
Converts the String[] of values for a given parameter in the HttpServletRequest into the desired type of Object. If a converter is declared using an annotation for the property (or getter/setter) then that converter will be used - if it does not convert to the right type an exception will be logged and values will not be converted. If no Converter was specified then a default converter will be looked up based on the target type of the property. If there is no default converter, then a Constructor will be looked for on the target type which takes a single String parameter. If such a Constructor exists it will be invoked.
Only parameter values that are non-null and do not equal the empty String will be converted and returned. So an input array with one entry equaling the empty string, [""], will result in an empty List being returned. Similarly, if a length three array is passed in with one item equaling the empty String, a List of length two will be returned.
bean
- the ActionBean on which the property to convert existspropertyName
- the name of the property being convertedvalues
- a String array of values to attempt conversion ofdeclaredType
- the declared type of the ActionBean propertyscalarType
- if the declaredType is a collection, map or array then this will
be the type contained within the collection/map value/array, otherwise
the same as declaredTypevalidationInfo
- the validation metadata for the property if definederrors
- a List into which ValidationError objects will be populated for any errors
discovered during conversion.
protected String[] trim(String[] values, ValidationMetadata meta)
ValidationMetadata
object to determine if the given values
should be trimmed. If so, then the trimmed values are returned. Otherwise, the values are
returned unchanged. If meta
is null, then the default action is taken, and the values
are trimmed. Either values
or meta
(or both) may be null.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |