@duyao
2015-05-24T14:02:25.000000Z
字数 2919
阅读 1973
jstl
jstl=jsp standard tag library,产生jstl的原因是减少java代码片段,提高开发效率
要先引包,才能使用
首先要保证有jstl-1.2.jar的包,然后点开后,有很多包,找到META-INF
,找到*.tld
文件,会有uri
这个标签,将此uri写入taglib
中的uri
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
prefix
是指标签名字,就是c.tld
,fn.tld
等
用于输出
①
您的用户名是: <c:out value=”${user.username}” default=”guest”/>
显示用户的用户名,如为空则显示guest
②
<c:out value="${sessionScope.username}"/>
指定从session中获取username的值显示;
③
<c:out value="${username}" />
显示username的值,默认是从request(page)中取,如果request中没有名为username的对象则从session中取,session中没有则从application(servletContext)中取,如果没有取到任何值则不显
④
escapeXml属性是指逃离对特殊编码的处理,默认为true,即对于html等标签不做任何处理
<c:out value='click<a href="www.baidu.com">baidu</a>' default="no value" escapeXml="false"/>
语法
<c:set value="value" var="varName" [scope="{page|request|session|application}"]/>
直接设置
<c:set value="10000" var="maxUser" scope="application"/>
<c:set value="20" var="maxIdelTime" scope="session"/>
<c:set value="next.jsp" var="nextPage" scope="page"/>
通过属性设置
<% Foo foo = new Foo(); pageContext.setAttribute("foo", foo); %>
<c:out value="${foo.date}"/><br> <c:set target="${foo}" property="day" value="1"/>
<c:out value="${foo.date}"/>
redirect
可以将客户端的请求从一个 JSP 网页导向到其他文件。
param
用于传递参数,用来为包含或重定向的页面传递参数。
<c:redirect url="/MyHtml.jsp">
<c:param name="userName" value=”RW” />
</c:redirect>
<c:remove var="username" scope="session"/>
将username
变量从session
范围移除。若我们不设定scope
,则<c:remove>
会移除所有范围名称为username的数据。
判断作用,一般与其他标签一起使用
<c:if test="${empty param.empDate}">
<jsp:forward page="input.jsp">
<jsp:param name="msg" value="Missing the Employment Date" />
</jsp:forward>
</c:if>
语法结构
<c:choose>
body(<when>和<otherwise>)
</c:choose>
<c:choose>
<c:when test="${condition1}">
condition1 为 true
</c:when>
<c:when test="${condition2}">
condition2 为 true
</c:when>
<c:otherwise>
condition1和conditon2都为false
</<c:otherwise >
</c:choose>
有两种方法:一种是普通遍历,基于次数begin
和end
<c:forEach [var="var"] [varStatus="varStatus"] begin="startIndex" end="stopIndex" [step="increment"]>
JSP elements
</c:forEach>
另一种基于集合元素进行迭代item
注意:这里迭代取值的时候,取出其属性用.
来取,这个属性一定是用getXxx()
和setXxx()
得到的,而不一定使其真正的名名字,具体意思是:比如student
有属性mystuid
和getStuid()
,而在el表达式中就一定要用{s.stuid}
比如下面的map中${mymap.key}
jdk中getKey()
的方法
<%
Map map = new HashMap();
map.put("a", "12345");
map.put("b", "abcde");
out.println(map.get("a"));
request.setAttribute("map",map);
%>
<!-- 完全迭代 -->
<c:forEach items="${map}" var="mymap" >
<c:out value="${mymap.key}" />
<c:out value="${mymap.value}" />
</c:forEach>
<!-- 根据map的key来找到特定的值 -->
<c:forEach items="${map}" var="mymap">
<c:if test="${mymap.key=='a'}">
<c:out value="${mymap.value}"/>
</c:if>
</c:forEach>
<c:forEach items="${arrayList}" var="s">
<tr>
<td >${s.stuid} </td>
<td >${s.name}</td>
</tr>
</c:forEach>
forTokens用来处理字符串
<%
String phonenumber="123-456-7899";
request.setAttribute("userPhone",phonenumber);
%>
<c:forTokens items="${userPhone}" delims="-" var="item">
${item}
</c:forTokens>
很少使用