简体   繁体   中英

struts ognl dynamic expression

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.

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}" />');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM