markzzz
markzzz

Reputation: 47945

Struts2 - Including a jsp according to the bean values

I need a sort of page switch on Struts2.

Like (on index.jsp) if myBean.String="main" include main.jsp else include welcome.jsp

I tried with <s:if> or <c:choose> but looks that they can evalutate only boolean. How can I do it?

Upvotes: 0

Views: 859

Answers (1)

BalusC
BalusC

Reputation: 1108742

Not sure about the Struts2 part, but you can just use EL in <jsp:include>.

<jsp:include page="${bean.pagename}.jsp" />

As to the JSTL <c:if> or <c:choose> tags, you can just compare strings in EL as follows (like as in JSF which you're already familiar with, according to your question history! ;) ):

<c:choose>
    <c:when test="${bean.pagename == 'main'}">
        <jsp:include page="main.jsp" />
    </c:when>
    <c:otherwise>
        <jsp:include page="welcome.jsp" />
    </c:otherwise>
</c:choose>

If it are only 2 conditions, then you can also use the conditional operator ?::

<jsp:include page="${bean.pagename == 'main' ? 'main' : 'welcome'}.jsp}" />

See also:

Upvotes: 2

Related Questions