Greetings,

Using Struts2, 2.0.11 and Java 5, and having problems with custom type
conversion when I type invalid data in a field.  I want to convert times in
military format (0700, 2205) to equivalent java.util.Date, with the date
fields set to 1-1-1970.  I am using the defaultStack, which includes
prepare, params, conversionError, validation, and workflow interceptors in
the standard order.

If I enter valid data, everything works properly and I end up with nice Date
fields.  If I enter something invalid (e.g. 1599, 99 isn't a valid minutes
value), the error is detected and reported, but the original text isn't
displayed in the input field.  I'm not sure what I did wrong; could someone
please help?

When I type invalid data in a field, the following happens:

* Type conversion fails (good)
* Error is added to the error list (good)
* Workflow interceptor sees the error and returns "input" (good)
* Error appears properly in my <s:fielderror/> section (good)
* Text I originally typed in the field does NOT appear in the text box (not
good) 
* I have debug logging on and I see the following lines:

[2008/Mar/19 12:13:00] [DEBUG] (ParametersInterceptor:148) Setting params
someBeanList[0].endTime => [ 1599 ] someBeanList[0].startTime => [ 0700 ] 

then a few lines later:

[2008/Mar/19 12:13:00] [DEBUG] (XWorkConverter:278) field-level type
converter for property [endTime] =
[EMAIL PROTECTED]
[2008/Mar/19 12:13:00] [DEBUG] (OgnlValueStack:165) Error setting value
ognl.MethodFailedException: Method "setEndTime" failed for object
[EMAIL PROTECTED] Mar 03 00:00:00 EST
2008,startTime=<null>,endTime=<null>,invEndTime=<null>]
[java.lang.NoSuchMethodException: setEndTime([Ljava.lang.String;)]
        at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:823)
        at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:964)
        at
ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:75)
        at 
ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131)
        at
com.opensymphony.xwork2.util.OgnlValueStack$ObjectAccessor.setProperty(OgnlValueStack.java:68)
        at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
        at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
        at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
        at ognl.SimpleNode.setValue(SimpleNode.java:246)
        at ognl.ASTChain.setValueBody(ASTChain.java:172)
        at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
        at ognl.SimpleNode.setValue(SimpleNode.java:246)
        at ognl.Ognl.setValue(Ognl.java:476)
        at com.opensymphony.xwork2.util.OgnlUtil.setValue(OgnlUtil.java:186)
... snip ...

I logged the contents of the ActionContext later and see this:

original.property.override={driverDays[0].endTime='1599'}

So it looks like the conversionError interceptor worked properly.

Action class:
private List<SomeBean> someBeanList;  // ArrayList

SomeBean class:
private Date startTime;
private Date endTime;

// getters and setters here

SomeBean-conversion.properties:
startTime=com.package.converter.TimeConverter
endTime=com.package.converter.TimeConverter

TimeConverter:

public class TimeConverter extends StrutsTypeConverter {

    @SuppressWarnings("unchecked")
    @Override
    public Object convertFromString(Map context, String[] paramValues, Class
toClass) {
        String timeString = paramValues[0];
        
        DateFormat df = getDateFormat();
        
        try {
            return df.parse(timeString);
        } catch (ParseException pe) {
            throw new TypeConversionException(pe);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public String convertToString(Map context, Object objToConvert) {
        if (objToConvert == null) {
            return ""; //$NON-NLS-1$
        }
        
        DateFormat df = getDateFormat();
        return df.format((Date)objToConvert);
    }
    
    private DateFormat getDateFormat() {
        DateFormat df = new SimpleDateFormat("HHmm"); //$NON-NLS-1$
        df.setLenient(false);
        return df;
    }


JSP:
(adapted from Roughley's Starting Struts 2, p.64)

<s:iterator value="someBeanList" status="stat">
<s:text name="startTime" />
<s:textfield name="someBeanList[%{#stat.index}].startTime" 
                value="%{someBeanList[#stat.index].startTime}"  size="4" 
maxlength="4"
                onkeypress="return numbersonly(this, event)" />
<s:text name="endTime" />
<s:textfield name="someBeanList[%{#stat.index}].endTime"
                value="%{someBeanList[#stat.index].endTime}" size="4" 
maxlength="4"
                onkeypress="return numbersonly(this, event)" />
</s:iterator>



-- 
View this message in context: 
http://www.nabble.com/Type-conversion-OGNL-question-tp16147699p16147699.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to