Jsp使用if和jstl

时间:2014-11-28 10:03:24

标签: jsp java-ee servlets jstl

如何将此Java代码翻译为Jstl标记?谢谢

String var;
if(condition 1 == true)
   var="Hello1";
else
   var="Hello2"

2 个答案:

答案 0 :(得分:1)

您可以<c:choose><c:when>和&lt; c:otherwise&gt;

一起使用
<c:choose>
    <c:when test="#{condition1}">
        <c:set var="var" value="Hello1"/>
    </c:when>
    <c:otherwise>
        <c:set var="var" value="Hello2"/>
    </c:otherwise>
</c:choose>

Some docs

答案 1 :(得分:1)

答案:

<% 
pageContext.setAttribute("condtion", condtion);
%>
<c:choose>
   <c:when test="${condition == true }">
       <c:set var="var" value="Hello1" />
   </c:when>
   <c:otherwise>
    <c:set var="var" value="Hello2" />
   </c:otherwise>
</c:choose>