收集一些常用的代码
 

jstl 中c:when使用示例

问题驱动学习,掌握更快。

把下面jsp页面中的scriptlet换成具有相同功能的jstl片段:

if(var1.equalsIgnoreCase(var2)){

     some html stuff

}else{

      more html

}

 

使用 <c:choose> 完成此任务. equalsIgnoreCase()可以使用 fn:toLowerCase().作替换:

<c:choose>

    <c:when test="${fn:toLowerCase(var1) == fn:toLowerCase(var2)}">

        Both are equal.

    </c:when>

    <c:otherwise>

        Both are not equal.

    <c:otherwise>

</c:choose>

如果你使用的是Servlet 3.0 容器(Tomcat 7, Glassfish 3, JBoss AS 6, etc) ,在web.xml 声明了 Servlet 3.0,那么你也可以调用equalsIgnoreCase() 方法:

<c:choose>

    <c:when test="${var1.equalsIgnoreCase(var2)}">

        Both are equal.

    </c:when>

    <c:otherwise>

        Both are not equal.

    <c:otherwise>

</c:choose>

 

别忘了在页首添加<%@ taglib uri="https://java.sun.com/jsp/jstl/core" prefix="c"%>

2012-03-15 /
标签: Jstl
 
评论
© 代码|Powered by LOFTER