Jstl使用scriptlet

时间:2013-05-12 08:08:18

标签: java jsp servlets jstl

<%! int x=5; %>


        <c:choose>
            <c:when test="${x eq 5}"><p>hello1</p></c:when>            
            <c:when test="${x gt 10}"><p>}hello3</p></c:when>
            <c:otherwise>Value is ${x},Not hello</c:otherwise>
        </c:choose>

为什么上面的代码从我的jsp页面输出not hello?为什么它不给hello1作为输出?

1 个答案:

答案 0 :(得分:3)

因为${x}不评估本地变量和实例变量。它查找页面,然后是request-,然后是session-,然后是应用程序范围的名为“x”的属性。如果您使用

,上面的代码将起作用
<% pageContext.setAttribute("x", 5) %>

或者,因为应该避免使用scriptlet,因此更加清晰:

<c:set var="x" value="5" />