1.在jsp文件中引用
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
2.jstl常用标签的使用
1)choose
相当于switch
<body> <% Rat rat=new Rat(); rat.setName("Cally"); rat.setAge(10); request.setAttribute("rat1", rat); %> <c:choose> <c:when test="${rat1.age==7 }"> 老鼠年纪为7 </c:when> <c:when test="${rat1.age==8 }"> 老鼠年纪为8 </c:when> <c:when test="${rat1.age==10 }"> 老鼠年纪为10 </c:when> </c:choose> </body>
forEach
<% Map map=new HashMap(); map.put("aa", "宝玉"); map.put("cc", "黛玉"); request.setAttribute("persons", map); %> <c:forEach items="${persons }" var="per"> key=${per.key } value=${per.value} </c:forEach>
if
<body> <% request.setAttribute("a", "hello"); Rat rat=new Rat(); rat.setName("小宝"); rat.setAge(5); request.setAttribute("rat1", rat); %> <c:if test="${a=='hello' }"> ok! </c:if> <c:if test="${a!='hello' }"> no ok! </c:if> <c:if test="${rat1.age>3 }"> 老鼠年纪大于3岁 </c:if> </body>
out
<!-- 同时出现多个域对象,pageContext的优先级最高 pageContext=>request=>session=>application --> <% //request.setAttribute("abc", "<a href='http://www.baidu.com'>百度</a>"); //session.setAttribute("abc", "你好1"); //application.setAttribute("abc", "你好2"); //pageContext.setAttribute("abc", "你好3"); //将user初始化后,当做域对象 User user=new User(); user.setName("Cally"); user.setAge(30); request.setAttribute("user1", user); %> <!-- escapXml 用于指定是否按照html样式显示,默认true,表示文本,false为html格式 --> <c:out value="${abc}" escapeXml="false"></c:out> <!-- 将user1对象中的值取出来,调用了对象的get方法 --> <c:out value="${ user1.age}"></c:out> </body>
set
<body> <!-- 等价于 request.setAttrbute("abc","中国,北京"); --> <c:set var="abc" value="中国,北京" scope="request"></c:set> <c:out value="${abc} "></c:out> <c:remove var="abc"/> <c:out value="${abc}" default="没有了"></c:out> </body>