You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Maxx <ma...@gmail.com> on 2008/02/15 19:52:37 UTC

Unable to make Type Conversion work in Struts2

Hello,

Refering to http://struts.apache.org/2.x/docs/type-conversion.html ,
I'm currently trying to make Type Conversion working on a really
simple example. And unfortunately, nothing's working. Below are the
sources.

Could someone please tell what I'm doing wrong (I suspect something
with the OGNL expressions but could not found what...)

Greets,
Maxx

PS: the <s:iterator> doesn't display anything, but if you move the
testList under a JavaBean (e.g. TestBean) and you change testList to
testBean.testList as OGNL value everywhere it's used (esp. for the
iterator), it displays the values in the list... BUT the textfields
still do not work, as while submitting the form, there are some
Exceptions in the log which say:
ParametersInterceptor - [setParameters]: Unexpected Exception caught
setting 'testBean.testList(0)' on 'class
com.mycompany.somepacakge.action.TestAction: Error setting expression
'testBean.testList(0)' with value '[Ljava.lang.String;@102011a'

---

// TestAction.java
public class TestAction {

    public String execute() {

        List<String> testList = new ArrayList<String>(2);
        testList.add("LIST 1st VALUE");
        testList.add("LIST 2nd VALUE");
        setTestList(testList);

        return INPUT;

    }

    public List<String> getTestList() {
        return (List<String>) session.get("list");
    }

    public void setTestList(List<String> list) {
        session.put(list, "list");
    }


}


// TestAction-conversion.properties
Element_testList=java.lang.String


// test.jsp
<%@page language="java" contentType="text/html; charset=ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>

<s:form id="testForm" action="Test" method="post">
<pre>
	Direct access: = does not work
	testList(0) = <s:property value="testList(0)" />
	testList(1) = <s:property value="testList(1)" />

	Regular iterator: = does not work
<s:iterator value="testList" status="stat">
	testList(<s:property value="#stat.index"/>) = <s:property value="top"
/> // <s:textfield name="testList(%{#stat.index})" />
</s:iterator>
</pre>
	<s:submit id="submitBtn" value="Submit Test" action="Test" />
</s:form>

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


Re: Unable to make Type Conversion work in Struts2

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Hi Maxx,

Yeah, I've had no success with Enum's either for the same reason, 
however that's definitely fixed in Struts 2.1.1 (XWork 2.1)
In theory, EnumMap should work in 2.1 as well, although I'm not sure 
what can be done about the constructor.

regards,
 Jeromy Evans

Maxx wrote:
> Thanks for your answer.
>
> I partially fixed this, noticing the square brackets notation is the
> same for Lists and Maps, which is not so clear on the related doc page
> (I posted a comment about this, having had a conversion with Dave
> Newton about this in a more recent thread on the mailing-list), as
> well as fixed the getter method which had a bad copy/paste issue.
>
> But I found another problem that might need some better formulation
> from me, and eventually a Jira: it seems Struts2 isn't able to fully
> support the java.lang.EnumMap object (e.g. using a EnumMap<YourEnum,
> String>), given that it's a particular Map (i.e. all keys are known,
> and they are NOT Strings) without any no-arg constructor - thus
> resulting in some cases in an InstanciationException.
>
> I tried some work-around with the introduction of an
> "EnumTypeConverter" (extending StrutsTypeConverter), which seemed to
> work fine at first sight, but unfortunately there's a buggy side
> effect: Struts2 then tried to convert the input value (which has to be
> the "value" of the map of type java.lang.String, if referring to the
> above example) ... in the corresponding Enum type of the key!!!
> Even if its declared in the <youraction>-conversion.properties with:
> Element_xxx=java.lang.String
>
> ---------------------------------------------------------------------
> 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: Unable to make Type Conversion work in Struts2

Posted by Maxx <ma...@gmail.com>.
Thanks for your answer.

I partially fixed this, noticing the square brackets notation is the
same for Lists and Maps, which is not so clear on the related doc page
(I posted a comment about this, having had a conversion with Dave
Newton about this in a more recent thread on the mailing-list), as
well as fixed the getter method which had a bad copy/paste issue.

But I found another problem that might need some better formulation
from me, and eventually a Jira: it seems Struts2 isn't able to fully
support the java.lang.EnumMap object (e.g. using a EnumMap<YourEnum,
String>), given that it's a particular Map (i.e. all keys are known,
and they are NOT Strings) without any no-arg constructor - thus
resulting in some cases in an InstanciationException.

I tried some work-around with the introduction of an
"EnumTypeConverter" (extending StrutsTypeConverter), which seemed to
work fine at first sight, but unfortunately there's a buggy side
effect: Struts2 then tried to convert the input value (which has to be
the "value" of the map of type java.lang.String, if referring to the
above example) ... in the corresponding Enum type of the key!!!
Even if its declared in the <youraction>-conversion.properties with:
Element_xxx=java.lang.String

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


Re: Unable to make Type Conversion work in Struts2

Posted by Jeromy Evans <je...@blueskyminds.com.au>.
Did you ever resolve this?

I always use a Map rather than a List because historically there was a 
problem accessing Lists via the OGNL implementation.
I don't know whether it was fixed

ie. Substitute the List with a Map and address the items by the 
appropriate key instead of the index

<s:property value="testList['0']" 

Moving to Map of course loses order but the Indexed Map conversion does 
work as stated in the docs.

Maxx wrote:
> Hello,
>
> Refering to http://struts.apache.org/2.x/docs/type-conversion.html ,
> I'm currently trying to make Type Conversion working on a really
> simple example. And unfortunately, nothing's working. Below are the
> sources.
>
> Could someone please tell what I'm doing wrong (I suspect something
> with the OGNL expressions but could not found what...)
>
> Greets,
> Maxx
>
> PS: the <s:iterator> doesn't display anything, but if you move the
> testList under a JavaBean (e.g. TestBean) and you change testList to
> testBean.testList as OGNL value everywhere it's used (esp. for the
> iterator), it displays the values in the list... BUT the textfields
> still do not work, as while submitting the form, there are some
> Exceptions in the log which say:
> ParametersInterceptor - [setParameters]: Unexpected Exception caught
> setting 'testBean.testList(0)' on 'class
> com.mycompany.somepacakge.action.TestAction: Error setting expression
> 'testBean.testList(0)' with value '[Ljava.lang.String;@102011a'
>
> ---
>
> // TestAction.java
> public class TestAction {
>
>     public String execute() {
>
>         List<String> testList = new ArrayList<String>(2);
>         testList.add("LIST 1st VALUE");
>         testList.add("LIST 2nd VALUE");
>         setTestList(testList);
>
>         return INPUT;
>
>     }
>
>     public List<String> getTestList() {
>         return (List<String>) session.get("list");
>     }
>
>     public void setTestList(List<String> list) {
>         session.put(list, "list");
>     }
>
>
> }
>
>
> // TestAction-conversion.properties
> Element_testList=java.lang.String
>
>
> // test.jsp
> <%@page language="java" contentType="text/html; charset=ISO-8859-1"%>
> <%@taglib prefix="s" uri="/struts-tags"%>
>
> <s:form id="testForm" action="Test" method="post">
> <pre>
> 	Direct access: = does not work
> 	testList(0) = <s:property value="testList(0)" />
> 	testList(1) = <s:property value="testList(1)" />
>
> 	Regular iterator: = does not work
> <s:iterator value="testList" status="stat">
> 	testList(<s:property value="#stat.index"/>) = <s:property value="top"
> /> // <s:textfield name="testList(%{#stat.index})" />
> </s:iterator>
> </pre>
> 	<s:submit id="submitBtn" value="Submit Test" action="Test" />
> </s:form>
>
> ---------------------------------------------------------------------
> 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