Uploaded image for project: 'Struts 2'
  1. Struts 2
  2. WW-3936

Custom Struts2 type converter is only called in one direction

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.3.4.1
    • Fix Version/s: 2.3.28
    • Component/s: Core Actions
    • Labels:
      None
    • Environment:

      JDK 1.7, Tomcat 7.0.26

      Description

      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.

      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;
      	}
      }
      
      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;
      	}
      }
      
      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>
      
      xwork-conversion.properties
      eu.inform.presentation.ThemeDescriptor=eu.inform.integration.converter.ThemeDescriptorConverter
      

        Attachments

          Activity

            People

            • Assignee:
              lukaszlenart Lukasz Lenart
              Reporter:
              s.goetz Sebastian Götz

              Dates

              • Created:
                Updated:
                Resolved:

                Issue deployment