You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "CRANFORD, CHRIS" <Ch...@setech.com> on 2015/04/24 15:16:01 UTC

Problem using TypeConverters with SelectTag

I have defined a simple POJO and type converter shown here:

// Model POJO
public class SiteSelection {
                private String type;
                private String label;
                private List<Long> ids = new ArrayList<Long>();
                /* getter & setters */
}

// Converter
public class SiteSelectionTypeConverter extends StrutsTypeConverter {
                @Override
                public Object convertFromString(Map context, String[] values, Class toClass) {
                                /* converts values[0] from '{type}:{comma-separted list of ids} */
                }
                @Override
                public String convertToString(Map context, Object object) {
                                /* converts object to string using format '{type}:{comma-separated list of ids} */
                }
}

// Action
public class SomeAction extends ActionSupport implements Preparable {
                private List<SiteSelection> siteSelections;
                private SiteSelection selectedSite;
                /* getter & setters */
}

In the JSP, I have used the select tag as follows

<s:select name="selectedSite" list="siteSelections" />

The type converters get used and the select is populated with the appropriate key/value pairs.   The problem is when I take a specific entry in the siteSelections and set it on the selectedSite property during the initial render or populate selectedSite and re-render the input page during say a validation failure, the select tag doesn't apply the "selected" attribute on any options.

This seems to be an issue with how parameters.nameValue is being determined in the taglib java code and the fact that the template compares this parameter against itemKey rather than itemKeyStr.  In the case of using type conversion, the itemKey is something like "com.app.SiteSelection@2b3a51e" where as itemKeyStr is "A:1,2,3".

Can someone explain to me what was the intended way to use TypeConverter implementations with a select tag?  The only way I've been able to hack around this issue is

<s:set var="selectedSiteStr" value="selectedSite" />
<s:select name="selectedSite" value="#selectedSiteStr" list="siteSelections" template="my-custom-select" />

and modifying the select.ftl line 107 from:

<#if tag.contains(parameters.nameValue, itemKey) == true>

To:

<#if tag.contains(parameters.nameValue, itemKeyStr) == true>

But I'm not sure what side effects such a change would have.

___________________________________________________________
Chris Cranford
SAP/Oracle/J2EE Applications Developer
SETECH Inc & Companies
903 Industrial Drive, Murfreesboro TN 37129
Phone: (615) 890-1755 x361, Fax: (615) 890-9057, Mobile: (704) 650-1042
Email: chris.cranford@setech.com<ma...@setech.com>


Re: Problem using TypeConverters with SelectTag

Posted by Lukasz Lenart <lu...@apache.org>.
Chris

Isn't it the same issue https://issues.apache.org/jira/browse/WW-3936 ?


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

2015-04-30 16:43 GMT+02:00 CRANFORD, CHRIS <Ch...@setech.com>:
> Honestly, if a converter is going to be used when an <option/> tag is rendered, the <select/> tag should use the same logic for determining the selected value from the value stack.  It's prudent these two tags act unanimously on the data rather than opposing one another as they do now.
>
> Let us assume we have a database record that has a composite key.  One might create your list of values in the action as follows:
>
> public class RecordPk {
>   private Long field1;
>   private Long field2;
> }
>
> public class MyAction {
>   private Map<RecordPk, String> choices = new LinkedHashMap<RecordPk, String>();
>   private RecordPk selection;
> }
>
> <s:select name="selection" list="choices" />
>
> Using a StrutsTypeConverter I would see:
>
> <select name="selection">
>   <option value="1-29393">My Label</option>
>   <option value="3-39949">Another Label</option>
> </select>
>
> The problem is that the type converter is applied when creating the options and when the data is submitted, but the select never defaults the selected choice if a selection was initially set in the view model or when the action returns the user to input after a validation failure; thus losing any value the user had previously selected.
>
> I don't see this necessarily as a specific use case but just someone trying to use the technology as it was intended imo.
>
> -----Original Message-----
> From: Lukasz Lenart [mailto:lukaszlenart@apache.org]
> Sent: Thursday, April 30, 2015 8:30 AM
> To: Struts Users Mailing List
> Subject: Re: Problem using TypeConverters with SelectTag
>
> 2015-04-30 15:22 GMT+02:00 CRANFORD, CHRIS <Ch...@setech.com>:
>> Was this part of the fix in WW-4427?
>
> Rather unrelated to your problem as tags don't use context to store values. I'm still thinking if your solution is right, I mean common to be used by any other or rather is your specific use case.
>
>
> Regards
> --
> Łukasz
> + 48 606 323 122 http://www.lenart.org.pl/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>
> Email secured by Check Point

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Problem using TypeConverters with SelectTag

Posted by "CRANFORD, CHRIS" <Ch...@setech.com>.
Honestly, if a converter is going to be used when an <option/> tag is rendered, the <select/> tag should use the same logic for determining the selected value from the value stack.  It's prudent these two tags act unanimously on the data rather than opposing one another as they do now.

Let us assume we have a database record that has a composite key.  One might create your list of values in the action as follows:

public class RecordPk {
  private Long field1;
  private Long field2;
}

public class MyAction {
  private Map<RecordPk, String> choices = new LinkedHashMap<RecordPk, String>();
  private RecordPk selection;
}

<s:select name="selection" list="choices" />

Using a StrutsTypeConverter I would see:

<select name="selection">
  <option value="1-29393">My Label</option>
  <option value="3-39949">Another Label</option>
</select>

The problem is that the type converter is applied when creating the options and when the data is submitted, but the select never defaults the selected choice if a selection was initially set in the view model or when the action returns the user to input after a validation failure; thus losing any value the user had previously selected.  

I don't see this necessarily as a specific use case but just someone trying to use the technology as it was intended imo.

-----Original Message-----
From: Lukasz Lenart [mailto:lukaszlenart@apache.org] 
Sent: Thursday, April 30, 2015 8:30 AM
To: Struts Users Mailing List
Subject: Re: Problem using TypeConverters with SelectTag

2015-04-30 15:22 GMT+02:00 CRANFORD, CHRIS <Ch...@setech.com>:
> Was this part of the fix in WW-4427?

Rather unrelated to your problem as tags don't use context to store values. I'm still thinking if your solution is right, I mean common to be used by any other or rather is your specific use case.


Regards
--
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Email secured by Check Point

Re: Problem using TypeConverters with SelectTag

Posted by Lukasz Lenart <lu...@apache.org>.
2015-04-30 15:22 GMT+02:00 CRANFORD, CHRIS <Ch...@setech.com>:
> Was this part of the fix in WW-4427?

Rather unrelated to your problem as tags don't use context to store
values. I'm still thinking if your solution is right, I mean common to
be used by any other or rather is your specific use case.


Regards
-- 
Łukasz
+ 48 606 323 122 http://www.lenart.org.pl/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


RE: Problem using TypeConverters with SelectTag

Posted by "CRANFORD, CHRIS" <Ch...@setech.com>.
Was this part of the fix in WW-4427?

-----Original Message-----
From: CRANFORD, CHRIS [mailto:Chris.Cranford@setech.com] 
Sent: Tuesday, April 28, 2015 8:30 AM
To: Struts Users Mailing List
Subject: RE: Problem using TypeConverters with SelectTag

Version 2.3.20 GA

-----Original Message-----
From: Lukasz Lenart [mailto:lukaszlenart@apache.org]
Sent: Tuesday, April 28, 2015 6:20 AM
To: Struts Users Mailing List
Subject: Re: Problem using TypeConverters with SelectTag

Struts version? I thought this was already resolved.

2015-04-24 15:16 GMT+02:00 CRANFORD, CHRIS <Ch...@setech.com>:
> I have defined a simple POJO and type converter shown here:
>
> // Model POJO
> public class SiteSelection {
>                 private String type;
>                 private String label;
>                 private List<Long> ids = new ArrayList<Long>();
>                 /* getter & setters */ }
>
> // Converter
> public class SiteSelectionTypeConverter extends StrutsTypeConverter {
>                 @Override
>                 public Object convertFromString(Map context, String[] values, Class toClass) {
>                                 /* converts values[0] from '{type}:{comma-separted list of ids} */
>                 }
>                 @Override
>                 public String convertToString(Map context, Object object) {
>                                 /* converts object to string using format '{type}:{comma-separated list of ids} */
>                 }
> }
>
> // Action
> public class SomeAction extends ActionSupport implements Preparable {
>                 private List<SiteSelection> siteSelections;
>                 private SiteSelection selectedSite;
>                 /* getter & setters */ }
>
> In the JSP, I have used the select tag as follows
>
> <s:select name="selectedSite" list="siteSelections" />
>
> The type converters get used and the select is populated with the appropriate key/value pairs.   The problem is when I take a specific entry in the siteSelections and set it on the selectedSite property during the initial render or populate selectedSite and re-render the input page during say a validation failure, the select tag doesn't apply the "selected" attribute on any options.
>
> This seems to be an issue with how parameters.nameValue is being determined in the taglib java code and the fact that the template compares this parameter against itemKey rather than itemKeyStr.  In the case of using type conversion, the itemKey is something like "com.app.SiteSelection@2b3a51e" where as itemKeyStr is "A:1,2,3".
>
> Can someone explain to me what was the intended way to use 
> TypeConverter implementations with a select tag?  The only way I've 
> been able to hack around this issue is
>
> <s:set var="selectedSiteStr" value="selectedSite" /> <s:select 
> name="selectedSite" value="#selectedSiteStr" list="siteSelections"
> template="my-custom-select" />
>
> and modifying the select.ftl line 107 from:
>
> <#if tag.contains(parameters.nameValue, itemKey) == true>
>
> To:
>
> <#if tag.contains(parameters.nameValue, itemKeyStr) == true>
>
> But I'm not sure what side effects such a change would have.
>
> ___________________________________________________________
> Chris Cranford
> SAP/Oracle/J2EE Applications Developer SETECH Inc & Companies
> 903 Industrial Drive, Murfreesboro TN 37129
> Phone: (615) 890-1755 x361, Fax: (615) 890-9057, Mobile: (704)
> 650-1042
> Email: chris.cranford@setech.com<ma...@setech.com>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Email secured by Check Point

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org

RE: Problem using TypeConverters with SelectTag

Posted by "CRANFORD, CHRIS" <Ch...@setech.com>.
Version 2.3.20 GA

-----Original Message-----
From: Lukasz Lenart [mailto:lukaszlenart@apache.org] 
Sent: Tuesday, April 28, 2015 6:20 AM
To: Struts Users Mailing List
Subject: Re: Problem using TypeConverters with SelectTag

Struts version? I thought this was already resolved.

2015-04-24 15:16 GMT+02:00 CRANFORD, CHRIS <Ch...@setech.com>:
> I have defined a simple POJO and type converter shown here:
>
> // Model POJO
> public class SiteSelection {
>                 private String type;
>                 private String label;
>                 private List<Long> ids = new ArrayList<Long>();
>                 /* getter & setters */ }
>
> // Converter
> public class SiteSelectionTypeConverter extends StrutsTypeConverter {
>                 @Override
>                 public Object convertFromString(Map context, String[] values, Class toClass) {
>                                 /* converts values[0] from '{type}:{comma-separted list of ids} */
>                 }
>                 @Override
>                 public String convertToString(Map context, Object object) {
>                                 /* converts object to string using format '{type}:{comma-separated list of ids} */
>                 }
> }
>
> // Action
> public class SomeAction extends ActionSupport implements Preparable {
>                 private List<SiteSelection> siteSelections;
>                 private SiteSelection selectedSite;
>                 /* getter & setters */ }
>
> In the JSP, I have used the select tag as follows
>
> <s:select name="selectedSite" list="siteSelections" />
>
> The type converters get used and the select is populated with the appropriate key/value pairs.   The problem is when I take a specific entry in the siteSelections and set it on the selectedSite property during the initial render or populate selectedSite and re-render the input page during say a validation failure, the select tag doesn't apply the "selected" attribute on any options.
>
> This seems to be an issue with how parameters.nameValue is being determined in the taglib java code and the fact that the template compares this parameter against itemKey rather than itemKeyStr.  In the case of using type conversion, the itemKey is something like "com.app.SiteSelection@2b3a51e" where as itemKeyStr is "A:1,2,3".
>
> Can someone explain to me what was the intended way to use 
> TypeConverter implementations with a select tag?  The only way I've 
> been able to hack around this issue is
>
> <s:set var="selectedSiteStr" value="selectedSite" /> <s:select 
> name="selectedSite" value="#selectedSiteStr" list="siteSelections" 
> template="my-custom-select" />
>
> and modifying the select.ftl line 107 from:
>
> <#if tag.contains(parameters.nameValue, itemKey) == true>
>
> To:
>
> <#if tag.contains(parameters.nameValue, itemKeyStr) == true>
>
> But I'm not sure what side effects such a change would have.
>
> ___________________________________________________________
> Chris Cranford
> SAP/Oracle/J2EE Applications Developer SETECH Inc & Companies
> 903 Industrial Drive, Murfreesboro TN 37129
> Phone: (615) 890-1755 x361, Fax: (615) 890-9057, Mobile: (704) 
> 650-1042
> Email: chris.cranford@setech.com<ma...@setech.com>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Email secured by Check Point

Re: Problem using TypeConverters with SelectTag

Posted by Lukasz Lenart <lu...@apache.org>.
Struts version? I thought this was already resolved.

2015-04-24 15:16 GMT+02:00 CRANFORD, CHRIS <Ch...@setech.com>:
> I have defined a simple POJO and type converter shown here:
>
> // Model POJO
> public class SiteSelection {
>                 private String type;
>                 private String label;
>                 private List<Long> ids = new ArrayList<Long>();
>                 /* getter & setters */
> }
>
> // Converter
> public class SiteSelectionTypeConverter extends StrutsTypeConverter {
>                 @Override
>                 public Object convertFromString(Map context, String[] values, Class toClass) {
>                                 /* converts values[0] from '{type}:{comma-separted list of ids} */
>                 }
>                 @Override
>                 public String convertToString(Map context, Object object) {
>                                 /* converts object to string using format '{type}:{comma-separated list of ids} */
>                 }
> }
>
> // Action
> public class SomeAction extends ActionSupport implements Preparable {
>                 private List<SiteSelection> siteSelections;
>                 private SiteSelection selectedSite;
>                 /* getter & setters */
> }
>
> In the JSP, I have used the select tag as follows
>
> <s:select name="selectedSite" list="siteSelections" />
>
> The type converters get used and the select is populated with the appropriate key/value pairs.   The problem is when I take a specific entry in the siteSelections and set it on the selectedSite property during the initial render or populate selectedSite and re-render the input page during say a validation failure, the select tag doesn't apply the "selected" attribute on any options.
>
> This seems to be an issue with how parameters.nameValue is being determined in the taglib java code and the fact that the template compares this parameter against itemKey rather than itemKeyStr.  In the case of using type conversion, the itemKey is something like "com.app.SiteSelection@2b3a51e" where as itemKeyStr is "A:1,2,3".
>
> Can someone explain to me what was the intended way to use TypeConverter implementations with a select tag?  The only way I've been able to hack around this issue is
>
> <s:set var="selectedSiteStr" value="selectedSite" />
> <s:select name="selectedSite" value="#selectedSiteStr" list="siteSelections" template="my-custom-select" />
>
> and modifying the select.ftl line 107 from:
>
> <#if tag.contains(parameters.nameValue, itemKey) == true>
>
> To:
>
> <#if tag.contains(parameters.nameValue, itemKeyStr) == true>
>
> But I'm not sure what side effects such a change would have.
>
> ___________________________________________________________
> Chris Cranford
> SAP/Oracle/J2EE Applications Developer
> SETECH Inc & Companies
> 903 Industrial Drive, Murfreesboro TN 37129
> Phone: (615) 890-1755 x361, Fax: (615) 890-9057, Mobile: (704) 650-1042
> Email: chris.cranford@setech.com<ma...@setech.com>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org