Search code examples
jspstripes

Conditional execution inside JSP in Stripes


I want to test against the value of n actionBean variable and print different messages based on the results. My code right now is:

<% if ( ${actionBean.server.virtual} == true ) { %>
This is a virtual machine.<br/>
<% } else { %>
This is not a virtual machine.<br/>
<% } %>

The actionBean has a function getServer() which returns an object that has a getVirtual() function that return a Boolean.

The code is (obviously) not working - I didn't really expect it to, but I haven't been able to find documentation combining the "access an actionBean" part with the "conditional execution" part with the "inside the Stripes framework" part. I don't want to just do

Virtual machine? ${actionBean.server.virtual}<br/>

Even though I know this will work, it's not very human-friendly.


Solution

  • You're mixing a Java scriptlet with JSTL. Choose JSTL and use the jsp core taglib.

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%--Your jsp should start with the taglib declaration above.--%>
    
    <c:choose>
        <c:when test="${actionBean.server.virtual}">
            This is a virtual machine.<br/>
        </c:when>
        <c:otherwise>
            This is not a virtual machine.<br/>
        </c:otherwise>
    </c:choose>