0

I've spent the last few hours to get a Struts expression to work as I want but didn' t succeeded.

I want to execute a OGNL expression from a String.

By example, I have the following code :

<s:push value="#row">

    alert('<s:property value="id + ' ' + id" />');

</s:push>

This works perfectly giving me the id of the object twice.

This example

<s:push value="#row">

    <s:set name="expr" value="id + ' ' + id"/>
    alert('<s:property value="%{#expr}" />');

</s:push>

works fine too.

But now, if I create a method in my action :

public String getTest() {

    return "id + ' ' + id";
}

and in the JSP :

<s:set name="expr" value="getTest()"/>

<s:push value="#row">

    alert('<s:property value="%{#expr}" />');

</s:push>

The result is :

alert('id + ' ' + id'); 

which doesn't work because of quotes.

But why in my last example I have the String value and not the expression evaluated ?

I've tried a lot of possibilities, read articles, tutorials but didn't find a suitable answer to my problem.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143

1 Answers1

0

Struts 2 escapes the ActionMessages by default.

Try <s:property value="%{#expr}" escape="false"/>

UDPATE

Try

alert(<s:property value="%{#expr}" />);

Not

alert('<s:property value="%{#expr}" />');
PbxMan
  • 7,525
  • 1
  • 36
  • 40
  • I have no difference when adding escape="false". – user2810476 Sep 24 '13 at 10:57
  • this is a Javascript issue not Java – PbxMan Sep 24 '13 at 11:06
  • This is obviously not the problem because s:property is executed and replaced by a value. I don't care of the Javascript not working, I ask why s:property is not replaced by the same value as the second example. – user2810476 Sep 24 '13 at 11:18
  • Removing the JS quotes wouldn't matter anyway, and would only work if the value being alerted was guaranteed to be something legal in JS without them. With the quotes the expression would need to be JS-escaped. – Dave Newton Sep 24 '13 at 14:53