JSP标准标签库
1.核心标签库
1.1<c:out>标签
在*.jsp页面引入:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% pageContext.setAttribute("info", "hello");//设置page属性 %> <h3><c:out value="${info }" /></h3> <h3><c:out value="${hello }" default="没有该属性" /></h3> <h3><c:out value="${hello }">没有该属性!</c:out></h3>
结果:
hello
没有该属性
没有该属性!
1.2<c:set>标签
<c:set var="属性名称" target="属性名称" value="属性内容" scope="page|request|session|application"></c:set>
1.
<c:set var="name" value="china" scope="request" /> <h3>${name}</h3>
2.
<%
Person p = new Person();
request.setAttribute("person", p);
%>
<!--把value的值设置person对象中的属性name中 -->
<c:set value="mxz" target="person" property="name"/>
<h3>${person.name}</h3>
1.3<c:remove>标签
<c:set var="info" value="mxz" scope="request" /> <c:remove var="info" scope="request"/> <h3>${info}</h3> 结果:
1.4<c:catch>标签
主要用来处理程序中的异常,并进行异常的处理。
<c:catch var="errorMsg"> <!-- 异常产生 --> <% int x = 12 / 0; %> </c:catch> <h3>异常信息:${errorMsg }</h3> 结果: 异常信息:java.lang.ArithmeticException: / by zero
1.5<c:if>标签(重点)
<!-- 无标签体 -->
<c:if test="" var="存储判断结果" [scope="page|request|session|application"] />
<!-- 有标签体 -->
<c:if test=""></c:if>
<c:if test="${person.name }=='mxz'" var="info" scope="page"> <h3>欢迎${person.name }光临</h3> </c:if> <c:if test="${21 > 8 }" var="info2"> <h3>21 大于 8</h3> </c:if>
1.6<c:choose>、<c:when>、<c:otherwise>标签
<c:choose> <c:when test="${20 > 10 }"> <h3>20 大于 10</h3> </c:when> <c:when test="${perosn.age }==22"> <h3>${person.name }</h3> </c:when> <c:otherwise> <!--放在c:when之后--> <h3>没有一个条件满足!</h3> </c:otherwise> </c:choose>
1.7<c:forEach>标签(重点)
<% String info[] = {"mz", "mxz", "zm", "zxm"}; pageContext.setAttribute("ref", info);
list list = new ArrayList();
list.add()2;
list.add(34);
request.setAttribute("lis",list);
Map map = new HashMap();
map.put("z1","z");
map.put("z2","z2");
session.setAttribute("mp",map);
%>
<c:forEach items="${ref }" var="test">
${test }、
</c:forEach>
<c:forEach items="${ref }" var="test" begin="0" end="1">
${test }、
</c:forEach>
<c:forEach items="${ref }" var="test" step="2">
${test }、
</c:forEach>
<c:forEach items="${lis }" var="li">
${li }、 </c:forEach>
<c:forEach items="${mp }" var="mm">
${mm.key }--->${mm.value}
</c:forEach>
1.8<c:import>标签
可以将其他页面的内容包含进来一起显示,与<jsp:include>功能类似,不同的是<c:import>可以外部的页面!
例:
<c:import url="http://www.bloig.cn" charEncoding="UTF-8" />
结果:导入时只会把站点的html代码加载进来,而相应的图片显示会有问题。
<c:import url="http://www.bloig.cn" charEncoding="UTF-8" /> <c:import url="param.jsp" charEncoding="UTF-8"> <c:param name="city" value="Fuyang"/> <c:param name="national" value="china"/> </c:import>
<!--输出--> <h3>city:${param.city }</h3> <h3>city:${param.national }</h3>
1.9<c:url>标签
2.0客户端跳转
<!--方法1 --> response.sendRedirect("");//客户端跳转
<!--方法2 --> <!--context:若访问在同一web容器下的其他资源,设置时必须以"/"开头 -->
<c:redirect url="跳转的地址" context="上下文路径">
<c:param name="参数名称" value="参数值" />
</c:redirect>
Author