JSPL JSP标准标签库
作用:
- 和 EL 表达式一起,取代<% %>java代码
JSTL标签库
- 核心标签库(core)c.tld
- XML(x:操作xml的标签库)
- SQL(sql标签库) sql.tld
- FMT(fmt:国际化标签库)
- JSTL函数(EL函数)el fn.tld
范例
<body>
<h4>传统方式</h4>
<%
int a = 10;
if(a == 10){
out.print("哈哈");
%>
<%= a=10 %>
<%
}
%>
<h4>使用jstl的方式</h4>
<c:set var="i" value="10" scope="page"></c:set>
<c:if test="${ i eq 10}">
i = 10<!-- 相当于html文本 -->
</c:if>
</body>
JSTL的标签
- <c:out>
- <c:set>
- <c:remove>
- <c:catch>
- <c:if>
- <c:choose>
- <c:forEach>
- <c:forTokens>
- <c:param>
- <c:import>
- <c:url>
- <c:redict>
<c:out>
<body>
<h4>传统方式</h4>
<%= "Hello" %>
<%
int a = 10;
request.setAttribute("name","xy");
%>
<%= a %>
<h4>JSTL的方式</h4>
<c:out value="Hello"></c:out>
<c:out value="${ name }"></c:out>
<c:out value="${ city }" default="BeiJing"></c:out>
<c:out value="<a href='xxx'>超链接</a>" escapeXml="false"></c:out>
</body>
<c:set>
<body>
<h4>传统方式</h4>
<%
int a = 10;
pageContext.setAttribute("name","10",pageContext.REQUEST_SCOPE);
%>
<%
User user = new User();//设置Javabean类
user.setUsername("毛毛");
user.setPassword("123");
request.setAttribute("user",user);
%>
${ user.username }
<h4>JSTL方式</h4>
<c:set var="i" value="10" scope="request"></c:set>
${ i }
<c:set target="${ user }" value="萌萌" property="username"></c:set>
${ user.username }
</body>
<c:remove>
作用
用于删除各种web域中的属性
<body>
<h4>传统方式</h4>
<%
request.setAttribute("age",20);
// 删除age
request.removeAttribute("age");
%>
<h4>JSTL的方式</h4>
<c:set var="age" value="20" scope="request"></c:set>
<c:remove var="age" scope="request"/>
</body>
<c:catch>标签
<body>
<h4>传统方式</h4>
<%
try {
int d = 1/0;
}catch(Exception e){
e.printStackTrace();
}
%>
<h4>JSTL方式</h4>
<!-- 异常就会被捕获,保存page范围 e 对象中 -->
<c:catch var="e">
<%
String s = "abc";
s.charAt(4);
%>
</c:catch>
${e.message }
<c:catch var="myex" >
<%
String st = "abc";
st.charAt(4);
%>
</c:catch>
异常:<c:out value="${myex}" /> ${myex}<br/>
异常 myex.getMessage:<c:out value="${myex.message}" /><br />
异常 myex.getCause:<c:out value="${myex.cause}" /><br />
异常 myex.getStackTrace:<c:out value="${myex.stackTrace}" />
</body>
<c:if>
<body>
<h4>传统方式</h4>
<%
int a = 10;
if(a >= 10){
out.print("a >= 10");
}else{
out.print("a < 10");
}
%>
<h4>JSTL的方式</h4>
<c:set var="i" value="10" scope="page"></c:set>
<c:if test="${ i ge 10 }" var="x" scope="page">
i>=10
</c:if>
${ x }
<c:if test="${i lt 10 }">
i < 10
</c:if>
</body>
<c:choose>
<h4>JSTL方式</h4>
<c:set var="count" value="1"></c:set>
<c:choose>
<c:when test="${ count eq 0 }">
现在的值是0
</c:when>
<c:otherwise>
符合您要求的记录共有${ count }条
</c:otherwise>
</c:choose>
<c:forEach>
作用
对一个集合对象中的元素进行循环迭代操作,或者按指定的次数重复迭代执行标签体中的内容
<h4>遍历数组</h4>
<%
String[] arrs = {"xixi","haha","msms","xila"};
request.setAttribute("arrs",arrs);
%>
<c:forEach var="s" items="${ arrs }">
${ s }<br>
</c:forEach>
<h4>遍历集合</h4>
<%
List<String> list = new ArrayList<String>();
list.add("啦啦");
list.add("d");
list.add("s");
list.add("d");
request.setAttribute("list",list);
%>
<c:forEach var="s" items="${ list }">
${ s }
</c:forEach>
<h4>遍历Map集合</h4>
<%
Map<String, String> map = new HashMap<String, String>();
map.put("aa","meimei");
map.put("bb","fengfeng");
map.put("cc","liangliang");
request.setAttribute("map",map);
%>
<c:forEach var="entry" items="${ map }">
${ entry.key }---${ entry.value }
</c:forEach>
<h4>遍历对象的集合</h4>
<%
List<User> ulist = new List<User>();
ulist.add(new User("meimei","123"));
ulist.add(new User("meei","1234"));
ulist.add(new User("mei","12334"));
request.setAttribute("uList",uList);
%>
<c:forEach var="user" items="uList">
${ user.username }--${ user.password }<br>
</c:forEach>
<h4>迭代数据</h4>
<c:forEach var="i" begin="1" end="10" step="1">
${ i }
</c:forEach>
<h4>计算从1加到100的和</h4>
<c:set value="0" var="sum" scope="page"></c:set>
<c:forEach var="i" begin="1" end="100" step="1">
<c:set var="sum" value="${ sum + i }"></c:set>
</c:forEach>
${ sum }
<h4>遍历10到100的偶数,每到第三个数,显示红色</h4>
<c:forEach var="i" begin="10" end="100" step="2" varStatus="status">
<c:choose>
<c:when test="${ status.count % 3 eq 0 }">
<font color="red">${ i }</font>
</c:when>
<c:otherwise>
${ i }
</c:otherwise>
</c:choose>
</c:forEach>
<c:param> <c:import><c:url>
<h4>JSTL</h4>
<!-- 包含页面 -->
<c:import url="/jstl/choose.jsp" context="/day13" var="i" scope="page">
<c:param name="username" value="meimei"></c:param>
</c:import>
${ i }
</body>
<h4>JSTL</h4>
<c:url var="i" value="/jstl/choose.jsp" scope="request" context="/day13">
<c:param name="username" value="mei"></c:param>
</c:url>
<a href="${ i }">choose</a>
<c:redirect>
实现重定向
<h4>JSTL</h4>
<c:redirect url="/jstl/choose.jsp" context="/day13">
<c:param name="username" value="meid"></c:param>
</c:redirect>
</body>
<c:forTokens> 了解
用来切分字符串
<h4>分割字符串</h4>
<c:set var="i" value="aa,bb,cc" scope="page"></c:set>
<c:forTokens items="${ i }" delims="," var="x">
${ x }
</c:forTokens>