jstl标签库的配置
* 将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果单纯的使用EL表达式,不用拷贝这两个jar)
注意:jstl必须在能够支持j2ee1.4/servlet2.4/jsp2.0版本上的容器才能运行(因为jstl和EL表达式结合使用的),如果容器版本低于2.4那么标签的配置要在web.xml中配置(EL表达式用于去scope中的值,jstl只是将EL表达式的值显示出来)
<c:out>讲解:
后台:
request.setAttribute("hello","hello JSTL");
request.setAttribute("aa","<font color="red">welcome</font>");
前台:
<% taglib prefix="c" uri=http://java.sun.com/jsp/jstl/core%>
<c:out value="hello" /> //输出 hello
<c:out value="${hello}" /> //输出 hello JSTL 一般是如果取到空值时,我们想给一个默认值时,使用此种方式
<c:out value="${hello}" default="123" />
<c:out value="${hello}">123</c:out> //与上句相同效果
${hello} //输出 hello JSTL 一般用此种方式,简洁
<c:out value="${aa}" /> //输出 <font color="red">welcome</font>
<c:out value="${aa}" escapeXml="false" /> //输出 字体为红色的 welcome escapeXml 的值默认为true
${aa} //输出 字体为红色的 welcome
<c:set>讲解:
<c:set> 设置的功能很强大,它的设置是有范围的,默认是当前page [scope="{page|request|session|application}"]
后台:
前台:
<c:set name="temp" value="123" /> //默认设置到范围为page
temp: ${temp} //输出 123
<c:remove var="temp" /> //移处属性temp的值
<c:if>讲解:
后台:
request.setAttribute("v1",1);
request.setAttribute("v2",2);
request.setAttribute("v3",new ArrayList());
前台:
<c:if test="${v1 lt v2}" var="v"> //将v1和v2比较的结果放入变量v中
v=${v} //结果输出 v=true
</c:if>
<c:if test="${empty v3}">
v3为空 //结果输出 v3为空
</c:if>
注意,标签中的if else 由 <c:choose><c:when><c:otherwise>标签共同来完成
<c:forEach>讲解:
后台:
ArrayList userList = new ArrayList();
userList.add(user);
request.setAttribute("list",userList);
Map map = new HashMap();
map.put("k1","v1");
map.put("k2","v2");
request.setAttribute("mapValue",map);
前台:(所有记录输出)
<c:choose>
<c:when test="${empty list}">
没有数据
</c:when>
<c:otherwise>
<c:forEach items="${list}" var="user" varStatus="vs">
<c:when test="${vs.count % 2== 0}">
<tr bgcolor="red">
</c:when>
<c:otherwise>
<tr>
</c:otherwise>
</c:forEach>
</c:otherwise>
</c:choose>
----------------------------------------------------------------------------------------------------
部分记录输出
<c:choose>
<c:when test="${empty list}">
没有数据
</c:when>
<c:otherwise>
<c:forEach items="${list}" var="user" begin="2" end="8" step="2"> //即:从记录二开始输入(容器中的记录是从0开始的,其实我们真是看到的是第三条记录开始的),到记录八停止,中间输出的步长为二,即:输出记录2、4、6、8,步长默认为1
<c:when test="${vs.count % 2== 0}">
<tr bgcolor="red">
</c:when>
<c:otherwise>
<tr>
</c:otherwise>
</c:forEach>
</c:otherwise>
</c:choose>
--------------------------------------------------------------------------------------------------------
普通输出
<c:forEach begin="1" end="10">
a<br> //输出10行a
</c:forEach>
---------------------------------------------------------------------------------------------------------
输出map中的值
<c:forEach items="${mapValue}" var="v"> //此时的 v 为 Entry 对象
${v.key} = ${v.value}
</c:forEach>
结果:
k1=v1
k2=v2
<c:forTokens>标签讲解:
此标签用于输出逗号表达式,或其他分隔符间隔的表达式,与java中的 StringTokenizer 对象类似
后台:
request.setAttribute("str","1,2,3,4,5,6");
前台:
<c:forTokens items="${str}" var="s" delims=",">
${s}
</c:forTokens>
输出
1
2
3
4
5
6
<c:catch>标签介绍:
相当于我们java中的catch
前台:
用jstl标签之前的代码
<%
try{
Integer.parseInt("sdfsdfds");
}catch{
.....
}
%>
用jstl标签后的代码
<c:catch var="exinfo"> //var中存放抛出的异常信息
<%
Integer.parseInt("sdfsdfds");
%>
</c:catch>
${exinfo}
结果:
java.lang.NumberFormatException:For input string :"sdfsdfds"
<c:import>标签详解:
相当于include,用于引入页面
例如:
<c:import url=http://localhost:8080/login />
<c:url>和<c:param>标签详解:
<c:url>用于生成一个url,而如果我们需要在url后面附加参数时则可以使用,<c:param>标签
例如:
<c:url value=http://localhost:8080/jstlDemo/addUser.jsp var="v">
<c:param name="userName" value="jack" />
<c:param name="age" value="15" />
<c:url>
输出:
${v}
结果:
http://localhost:8080/jstlDemo/addUser.jsp?userName=jack&age=15
<c:redirect>标签讲解:
重定向标签
<c:redirect context="/login.do" url=""></c:redirect> //如果我们要访问的是同一个web server下的资源则用context参数,写上相对路径或者服务就可以了,如果不在同一个web server下,即:跨系统访问,则使用参数url,写上完整路径,url也可以用在同一个web server 地下。