Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this post is a bit old but a solution may be useful to others.</p> <p>The default converter of Struts does not seem to work properly. The field error even occurs with readonly fields populated from the action.</p> <p>I resolved it by defining my own converter:</p> <ol> <li><p>Create the converter class (using the date format you need):</p> <pre><code>public class StringToDateTimeConverter extends StrutsTypeConverter { // WARNING not safe in multi-threaded environments private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss"); public Object convertFromString(Map context, String[] strings, Class toClass) { if (strings == null || strings.length == 0 || strings[0].trim().length() == 0) { return null; } try { return DATETIME_FORMAT.parse(strings[0]); } catch (ParseException e) { throw new TypeConversionException("Unable to convert given object to date: " + strings[0]); } } public String convertToString(Map context, Object date) { if (date != null &amp;&amp; date instanceof Date) { return DATETIME_FORMAT.format(date); } else { return null; } } } </code></pre></li> <li><p>Put the conversion annotation on the property setter (or use a conversion.properties file)</p> <pre><code>@Conversion() public class MyEditAction { // ... @TypeConversion(converter="my.app.common.converter.StringToDateTimeConverter") public void setUploadedDate(Date uploadedDate) { this.uploadedDate = uploadedDate; } </code></pre></li> </ol> <p>In my case I did not need to edit it but wanted to specify the format in the jsp. I used an additional hidden field to keep the original value (another alternative would have been the use of Preparable interface):</p> <pre><code>&lt;s:textfield name="uploadedDateDisplay" value="%{getText('format.datetimesecond',{uploadedDate})}" size="70" disabled="true"/&gt; &lt;s:hidden name="uploadedDate" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload