Search code examples
struts2ognl

Struts OGNL if statement related to action class's variable doesn't work,


The problem is: Inside my action class, i have one variable:

 private String commentAdd = "yes";

And the action class goes to reslut.jsp, inside reslut.jsp i have:

<s:set name="allowAddComment" value="commentAdd"/>
<s:if test="%{#allowAddComment=='yes'}">
                    <script type="text/javascript">
                        window.close();
                    </script>
</s:if>

but it doesn't work, can some expert give me some suggestion? Thanks.


Solution

  • A few things.

    • The property needs to be exposed via a public getter (or in later versions of S2 as a public member, but better to use a getter).
    • Why use a string as a boolean? Just use a boolean.
    • Why set the property to a different variable? Just use the property.

    Are you sure this is really what you want? This will close the window as soon as that JavaScript is rendered. If that's okay, fine--although if that's the case, why bother rendering the window at all?