|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectognl.DefaultTypeConverter
com.opensymphony.xwork.util.XWorkConverter
public class XWorkConverter
XWorkConverter is a singleton used by many of the WebWork's Ognl extention points, such as InstantiatingNullHandler, XWorkListPropertyAccessor etc to do object conversion. Type conversion is great for situations where you need to turn a String in to a more complex object. Because the web is type-agnostic (everything is a string in HTTP), WebWork's type conversion features are very useful. For instance, if you were prompting a user to enter in coordinates in the form of a string (such as "3, 22"), you could have WebWork do the conversion both from String to Point and from Point to String.
Using this "point" example, if your action (or another compound object in which you are setting properties on) has a corresponding ClassName-conversion.properties file, WebWork will use the configured type converters for conversion to and from strings. So turning "3, 22" in to new Point(3, 22) is done by merely adding the following entry to ClassName-conversion.properties (Note that the PointConverter should impl the ognl.TypeConverter interface): point = com.acme.PointConverter Your type converter should be sure to check what class type it is being requested to convert. Because it is used for both to and from strings, you will need to split the conversion method in to two parts: one that turns Strings in to Points, and one that turns Points in to Strings. After this is done, you can now reference your point (using <ww:property value="post"/> in JSP or ${point} in FreeMarker) and it will be printed as "3, 22" again. As such, if you submit this back to an action, it will be converted back to a Point once again. In some situations you may wish to apply a type converter globally. This can be done by editing the file xwork-conversion.properties in the root of your class path (typically WEB-INF/classes) and providing a property in the form of the class name of the object you wish to convert on the left hand side and the class name of the type converter on the right hand side. For example, providing a type converter for all Point objects would mean adding the following entry: com.acme.Point = com.acme.PointConverter Type conversion should not be used as a substitute for i18n. It is not recommended to use this feature to print out properly formatted dates. Rather, you should use the i18n features of WebWork (and consult the JavaDocs for JDK's MessageFormat object) to see how a properly formatted date should be displayed. Any error that occurs during type conversion may or may not wish to be reported. For example, reporting that the input "abc" could not be converted to a number might be important. On the other hand, reporting that an empty string, "", cannot be converted to a number might not be important - especially in a web environment where it is hard to distinguish between a user not entering a value vs. entering a blank value. By default, all conversion errors are reported using the generic i18n key xwork.default.invalid.fieldvalue, which you can override (the default text is Invalid field value for field "xxx", where xxx is the field name) in your global i18n resource bundle. However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name. It is important to know that none of these errors are actually reported directly. Rather, they are added to a map called conversionErrors in the ActionContext. There are several ways this map can then be accessed and the errors can be reported accordingly.
XWorkBasicConverter
Field Summary | |
---|---|
static String |
CONVERSION_COLLECTION_PREFIX
|
static String |
CONVERSION_ERROR_PROPERTY_PREFIX
|
static String |
CONVERSION_PROPERTY_FULLNAME
|
protected HashMap |
defaultMappings
|
protected ognl.TypeConverter |
defaultTypeConverter
|
static String |
LAST_BEAN_CLASS_ACCESSED
|
static String |
LAST_BEAN_PROPERTY_ACCESSED
|
protected static org.apache.commons.logging.Log |
LOG
|
protected HashMap |
mappings
|
protected HashSet |
noMapping
|
protected ObjectTypeDeterminer |
objectTypeDeterminer
|
static String |
REPORT_CONVERSION_ERRORS
|
protected HashSet |
unknownMappings
|
Constructor Summary | |
---|---|
protected |
XWorkConverter()
|
Method Summary | |
---|---|
protected void |
addConverterMapping(Map mapping,
Class clazz)
Looks for converter mappings for the specified class and adds it to an existing map. |
static String |
buildConverterFilename(Class clazz)
|
protected Map |
buildConverterMapping(Class clazz)
Looks for converter mappings for the specified class, traversing up its class hierarchy and interfaces and adding any additional mappings it may find. |
Object |
convertValue(Map map,
Object o,
Class aClass)
|
Object |
convertValue(Map context,
Object target,
Member member,
String property,
Object value,
Class toClass)
Convert value from one form to another. |
static String |
getConversionErrorMessage(String propertyName,
OgnlValueStack stack)
|
protected Object |
getConverter(Class clazz,
String property)
|
static XWorkConverter |
getInstance()
|
ObjectTypeDeterminer |
getObjectTypeDeterminer()
|
protected void |
handleConversionException(Map context,
String property,
Object value,
Object object)
|
void |
loadConversionProperties(String propsName)
|
ognl.TypeConverter |
lookup(Class clazz)
Looks for a TypeConverter in the default mappings. |
ognl.TypeConverter |
lookup(String className)
Looks for a TypeConverter in the default mappings. |
void |
registerConverter(String className,
ognl.TypeConverter converter)
|
void |
registerConverterNotFound(String className)
|
static void |
resetInstance()
|
void |
setDefaultConverter(ognl.TypeConverter defaultTypeConverter)
|
static void |
setInstance(XWorkConverter instance)
|
void |
setObjectTypeDeterminer(ObjectTypeDeterminer determiner)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final org.apache.commons.logging.Log LOG
public static final String REPORT_CONVERSION_ERRORS
public static final String CONVERSION_PROPERTY_FULLNAME
public static final String CONVERSION_ERROR_PROPERTY_PREFIX
public static final String CONVERSION_COLLECTION_PREFIX
public static final String LAST_BEAN_CLASS_ACCESSED
public static final String LAST_BEAN_PROPERTY_ACCESSED
protected HashMap defaultMappings
protected HashMap mappings
protected HashSet noMapping
protected HashSet unknownMappings
protected ognl.TypeConverter defaultTypeConverter
protected ObjectTypeDeterminer objectTypeDeterminer
Constructor Detail |
---|
protected XWorkConverter()
Method Detail |
---|
public static String getConversionErrorMessage(String propertyName, OgnlValueStack stack)
public static XWorkConverter getInstance()
public static void setInstance(XWorkConverter instance)
public static String buildConverterFilename(Class clazz)
public static void resetInstance()
public void setDefaultConverter(ognl.TypeConverter defaultTypeConverter)
public Object convertValue(Map map, Object o, Class aClass)
convertValue
in class ognl.DefaultTypeConverter
public Object convertValue(Map context, Object target, Member member, String property, Object value, Class toClass)
convertValue
in interface ognl.TypeConverter
convertValue
in class ognl.DefaultTypeConverter
TypeConverter.convertValue(java.util.Map, java.lang.Object, java.lang.reflect.Member, java.lang.String, java.lang.Object, java.lang.Class)
public ognl.TypeConverter lookup(String className)
className
- name of the class the TypeConverter must handle
public ognl.TypeConverter lookup(Class clazz)
clazz
- the class the TypeConverter must handle
protected Object getConverter(Class clazz, String property)
protected void handleConversionException(Map context, String property, Object value, Object object)
public void registerConverter(String className, ognl.TypeConverter converter)
public void registerConverterNotFound(String className)
protected void addConverterMapping(Map mapping, Class clazz)
mapping
- an existing map to add new converter mappings toclazz
- class to look for converter mappings forprotected Map buildConverterMapping(Class clazz) throws Exception
clazz
- the class to look for converter mappings for
Exception
public void loadConversionProperties(String propsName) throws IOException
IOException
public ObjectTypeDeterminer getObjectTypeDeterminer()
public void setObjectTypeDeterminer(ObjectTypeDeterminer determiner)
determiner
-
|
WebWork Project Page | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |