输出:<c:out>
例:
<%
pageContext.setAttribute("info","<www.google.com>");
<h1>属性存在:<c:out value="${info}"/></h1>
<h1>属性不存在:<c:out value="${ref}" default="没有此内容!"/></h1>
<h1>属性不存在:<c:out value="${ref}" >没有此内容!</c:out></h1>
设置:<c:set>
<c:set>主要用来将属性保存在四种范围之中,语法如下:
1=设置属性:
<c:set var="属性名称" value= "属性内容"scope="page/request/session/application"/>
<c:set var="属性名称" scope="page/request/session/application">
属性内容
</c:set>
2=设置对象:
<c:set value="属性内容" target="属性名称" property="属性名称"/>
<c:set target="属性名称" property="属性名称"></c:set>
例:
<c:set var="info" value="Hello MLDN!" scope="request"/>
<h1> 属性内容: ${info}</h1>
注:通过<c:set>标签还可以进行,已有javaBean的复制操作。
<c:if>:
<c:if>主要用于判断,与在程序中使用的if语法是一样的。
语法:
<c:if test="判断条件" var="存储判断结果" scope="page/request/session/application" />
或
<c:if test="判断条件" var="存储判断结果" scope="page/request/session/application" >
满足条件时执行的语句。
</c:if>
例子:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head></head>
<body>
<c:if test="${param.ref=='mldn'}" var="res1" scope="page">
<h1>欢迎${param.ref}光临!</h1>
</c:if>
<c:if test="${10 < 30}" var="res2" scope="page">
<h1> 10比30小!</h1>
</c:if>
</body>
</html>
<c:choose> <c:when> <c:oherwise>:
语法:
<c:choose>
<c:when test="判断条件">
满足条件时执行的语句
</c:when>
<c:otherwise>
当所有的<c:when>不满足时,执行此标签体的内容!
</c:otherwise>
</c:choose>
例子:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head></head>
<body>
<%
pageContext.setAttribute("num",10);
%>
<c:choose>
<c:when test="${num==10}">
<h1>test的属性值是10!</h1>
</c:when>
<c:when test="${num==20}">
<h1>test的属性值是20!</h1>
</c:when>
<c:otherwise>
<h1>没有一个条件满足!</h1>
</c:otherwise>
</c:choose>
</body>
</html>
在jstl中最重要的两个标签当然属<c:if>和<c:forEach>了,其它的基本上都用不上。
<c:forEach>:
主要功能是讲集合中的成员进行迭代输出,与Iterator的功能一样。
语法:
<c:forEach var="每一个对象的属性名称" items="集合" varStaus="保存相关成员的信息" begin="开始" end="结束" step="步长">
具体的操作代码
</c:forEach>
例子:
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head></head>
<body>
<%
List all = new ArrayList();
all.add("liuhui1");
all.add("liuhui2");
all.add("liuhui3");
pageContext.setAttribute("ref",all);
%>
<h1>输出全部:
<c:forEach items="${ref}" var="mem">
${mem}、
</c:forEach></h1>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head></head>
<body>
<%
String info[] = {"liuhui1","liuhui2","liuhui3"};
pageContext.setAttribute("ref",info);
%>
<h1>输出全部:
<c:forEach items="${ref}" var="mem">
${mem}、
</c:forEach></h1>
<h1>输出全部:(间隔为2)
<c:forEach items="${ref}" var="mem" step="2">
${mem}、
</c:forEach></h1>
<h1>输出前两个:
<c:forEach items="${ref}" var="mem" begin="0" end="1">
${mem}、
</c:forEach></h1>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<%@ taglib prefix="c" uri="http://www.mldn.cn/jstl/core"%>
<html>
<head></head>
<body>
<%
Map map = new HashMap();
map.put("x1","liuhui1");
map.put("x2","liuhui2");
map.put("x3","liuhui3");
pageContext.setAttribute("ref",map);
%>
输出全部:
<c:forEach items="${ref}" var="mem">
<h1>${mem.key}----->${mem.value}</h1>
</c:forEach>
</body>
</html>