[prev in list] [next in list] [prev in thread] [next in thread] 

List:       struts-dev
Subject:    [jira] [Commented] (WW-3936) Custom Struts2 type converter is only called in one direction
From:       "Lukasz Lenart (JIRA)" <jira () apache ! org>
Date:       2016-02-27 10:03:18
Message-ID: JIRA.12618565.1354543390000.164550.1456567398097 () Atlassian ! JIRA
[Download RAW message or body]


    [ https://issues.apache.org/jira/browse/WW-3936?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15170526#comment-15170526 \
] 

Lukasz Lenart commented on WW-3936:
-----------------------------------

I have developed a small example how to use this
https://github.com/apache/struts-examples/tree/master/type-conversion

> 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
> Assignee: Lukasz Lenart
> 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)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic