jstl
要使用C标签,就要引入.
<%@ taglib%>
是要告诉容器,转译JSP,会对应URI属性的自定义函数。
prefix是前置名称。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
uri是指C标签的路径:
MyEclipse中的 java EE 5 àjstl-1.2.jaràWEB-INFàc.tld中有uri
prefix: 要使用的C标签
C标签:
u <c:out >
<c:out value="${abcdfe}" default="hello<a href='#'>nn</a>" escapeXml="true"></c:out>
value:要输入到界面 default:没有的话,默认输出的 escapeXml:输出的html标签是否要过滤
如果使用的有EL ${} 取出内置对象的数据.内置对象中key名相同,原则是从最小的出来,就结束。
pageContext < request < session < application(生命周期大小)
u <c:set>
<c:set var="abc" value="hello" scope="request"></c:set>
就是 在想要加的内置对象中加入setAttribute(var,value);
u <c:remove>
<c:remove var="abc" scope="request"/>
删除Attribute的对象。如果没有scope,那么就代表 内置对象中所有的”abc都删除。”
u <c:catch>
<c:catch var="myException">
<%int i=9/0; %>
</c:catch>
<c:out value="${myException.message}"></c:out>
捕捉错误的标签。
u <c:if>
<% request.setAttribute(“a”,”123”)%>
<c:if test="${a=='123'}">
a=123;
</c:if>
判断语句 if,如果test的属性值是true;就运行。但是没有 else
u <c:choose> <c:when> <c:otherwise>
<c:choose>
<c:when test="${a>14 && false}">hao de</c:when>
<c:when test="${a<15}">bu hao</c:when>
<c:otherwise>hao hao de </c:otherwise>
</c:choose>
很像switch() case dedault
u <c:forEach>
LinkedList linkedList=new LinkedList();
linkedList.add("a");
linkedList.add("b");
linkedList.add("c");
request.setAttribute("linkedList",linkedList);
<c:forEach items="${linkedList}" var="li">
<c:out value="${li}"></c:out>
</c:forEach>
把linkedList全部取出来,li相当于 一个对象String
<br/>
<c:forEach items="${linkedList}" begin="0" end="10" var="i" step="2">
<c:out value="${i}"></c:out>
</c:forEach>
begin相当于开始下标,end终下标 step是指 相隔多少跳一下
u <c:forTokens>
<c:forTokens items="nihao;nihao;wo gu hao;haha" delims=";" var="temp">
${temp}
</c:forTokens>
items是一个字符串,delims是分隔符,var是结果
<c:redirect>
<c:redirect url="http://www.baidu.com"></c:redirect>
重定向。
<c:import>
<c:import url="a.jsp">
<c:param name="a" value="xiaohai"></c:param>
</c:import>
把a.jsp插入到本页面。可以带 param的 在a.jsp中 {param.a}取出
jstl的细节问题。
怎么取hashMap中的对象
Poe poe1=new Poe("xiaoming","23");
Poe poe2=new Poe("xiaohua","24");
HashMap poes=new HashMap();
poes.put("1",poe1);
poes.put("2",poe2);
request.setAttribute("listPoe",poes);
<c:forEach items="${listPoe}" var="poe">
key=<c:out value="${poe.key}"></c:out>
value=<c:out value="${poe.value}"></c:out>
poe=<c:out value="${poe.value.name} ${poe.value.age} "></c:out>
<br/>
</c:forEach>
怎么取set中的数据
Set sets=new HashSet();
sets.add(poe1);
sets.add(poe2);
request.setAttribute("sets",sets);
<c:forEach items="${sets}" var="set">
name=<c:out value="${set.name}"></c:out>
age=<c:out value="${set.age}"></c:out>
</c:forEach>
怎么用<c:if>来表示 取出来的对象是否为空。
request.setAttribute("abc","123");
<c:if test="${empty abc}">
<c:out value="shi null"></c:out>
</c:if>
<c:if test="${!empty abc}">
<c:out value="bu hsi null"></c:out>
</c:if>
<c:if test="${empty abd}">
<c:out value="shi null2"></c:out>
</c:if>