[ 
https://issues.apache.org/jira/browse/WW-3936?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Lukasz Lenart updated WW-3936:
------------------------------
    Fix Version/s:     (was: 2.3.x)
                   2.3.25

> Custom Struts2 type converter is only called in one direction
> -------------------------------------------------------------
>
>                 Key: WW-3936
>                 URL: https://issues.apache.org/jira/browse/WW-3936
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Core Actions
>    Affects Versions: 2.3.4.1
>         Environment: JDK 1.7, Tomcat 7.0.26
>            Reporter: Sebastian Götz
>             Fix For: 2.3.25
>
>
> We have a custom converter implemented that should convert select items from 
> and to id. Now it seems that only when submitting the form the 
> convertFromString method gets called. Upon rendering the page the 
> convertToString gets never called. So while we can submit values to the model 
> the initial selection of the select list is wrong. After some googling I 
> found a work around by overriding the toString() method to return the id to 
> make it work. But this is not an option in other cases where we face the same 
> problems.
> More strange is, that if I render the same into a textfield tag, the 
> converter is triggered in both directions.
> {code:title=ThemeDscriptor.java}
> public class ThemeDescriptor implements Serializable, Cloneable, 
> Comparable<ThemeDescriptor>
> {
>       private final String id;
>       private final String displayName;
>       private final File directory;
>       private final String contextPath;
>       public ThemeDescriptor(final String p_id, final String p_displayName, 
> final File p_directory, final String p_contextPath)
>       {
>               id = p_id;
>               displayName = p_displayName;
>               directory = p_directory;
>               contextPath = p_contextPath;
>       }
>       public String getId()
>       {
>               return id;
>       }
>       public String getDisplayName()
>       {
>               return displayName;
>       }
>       public File getDirectory()
>       {
>               return directory;
>       }
>       public String getContextPath()
>       {
>               return contextPath;
>       }
> }
> {code}
> {code:title=ThemeDescriptorConverter.java}
> package eu.inform.integration.converter;
> import java.util.Map;
> import org.apache.log4j.LogMF;
> import org.apache.log4j.Logger;
> import com.opensymphony.xwork2.conversion.TypeConversionException;
> import eu.inform.presentation.ThemeDescriptor;
> import eu.inform.presentation.UIConstants;
> public class ThemeDescriptorConverter extends AbstractBaseConverter
> {
>       private final static Logger log = 
> Logger.getLogger(ThemeDescriptorConverter.class);
>       @SuppressWarnings("rawtypes")
>       @Override
>       public Object convertFromString(final Map p_context, final String[] 
> p_values, final Class p_toClass)
>       {
>               if ((p_values != null) && (p_values.length > 0))
>               {
>                       try
>                       {
>                               Map<String, ThemeDescriptor> themes = 
> UIConstants.getThemes();
>                               if (themes.containsKey(p_values[0]))
>                               {
>                                       return themes.get(p_values[0]);
>                               }
>                               throw new RuntimeException("No theme with id: " 
> + p_values[0]);
>                       }
>                       catch (Exception e)
>                       {
>                               LogMF.error(log, e, "Unable to convert {0} into 
> a ThemeDescriptor", new Object[] { p_values[0] });
>                               throw new TypeConversionException("Unable to 
> convert into a ThemeDescriptor: " + p_values[0], e);
>                       }
>               }
>               return null;
>       }
>       @SuppressWarnings("rawtypes")
>       @Override
>       public String convertToString(final Map p_context, final Object p_o)
>       {
>               try
>               {
>                       return ((ThemeDescriptor) p_o).getId();
>               }
>               catch (Exception e)
>               {
>                       LogMF.error(log, e, "Unable to convert {0} into a 
> string", new Object[] { p_o });
>               }
>               return null;
>       }
> }
> {code}
> {code:xml|title=JSP}
> <s:form method="POST" action="design" theme="simple">
> <div  style="width:95%;">
>       <table>
>               <tr>
>                       <td><s:text name="choose" /></td>
>                       <td><s:select key="design" id="design" 
> list="designList" listValue="value.displayName" /></td>
>                       <td><s:submit key="save" /></td>
>               </tr>
>       </table>
>       </div>
> </s:form>
> {code}
> {code:xml|title=xwork-conversion.properties}
> eu.inform.presentation.ThemeDescriptor=eu.inform.integration.converter.ThemeDescriptorConverter
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to