您的位置:首页 > Web前端 > JavaScript

Jstl表达式常用标签语法

2018-03-09 08:56 316 查看
核心标签是最常用的JSTL标签。引用核心标签库的语法如下:
<%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
c:out>用于在JSP中显示数据,就像<%=...>
<c:set>用于保存数据
<c:remove>用于删除数据
<c:catch>用来处理产生错误的异常状况,并且将错误信息储存起来
<c:if>与我们在一般程序中用的if一样
<c:choose>本身只当做<c:when>和<c:otherwise>的父标签
<c:when><c:choose>的子标签,用来判断条件是否成立
<c:otherwise><c:choose>的子标签,接在<c:when>标签后,当<c:when>标签判断为false时被执行
<c:import>检索一个绝对或相对URL,然后将其内容暴露给页面
<c:forEach>基础迭代标签,接受多种集合类型
<c:forTokens>根据指定的分隔符来分隔内容并迭代输出
<c:param>用来给包含或重定向的页面传递参数
<c:redirect>重定向至一个新的URL.
<c:url>使用可选的查询参数来创造一个URL
if、choose标签的简单用法吧: 
c:choose语法格式
<c:choose>
<c:whentest="<boolean>"/>
...
</c:when>
<c:whentest="<boolean>"/>
...
</c:when>
...
...
<c:otherwise>
...
</c:otherwise>
</c:choose>
<c:choose>标签没有属性。
<c:when>标签只有一个属性,在下表中有给出。
<c:otherwise>标签没有属性。


c:if语法格式
<c:iftest="<boolean>"var="<string>"scope="<string>">
...
</c:if>
属性描述是否必要默认值
test条件是无
var用于存储条件结果的变量否无
scopevar属性的作用域否page
来看看c:forEach标签的简单用法吧: 
c:forEach语法格式
<c:forEach
items="<object>"
begin="<int>"
end="<int>"
step="<int>"
var="<string>"
varStatus="<string>">

...
属性描述是否必要默认值
items要被循环的信息否无
begin开始的元素(0=第一个元素,1=第二个元素)否0
end最后一个元素(0=第一个元素,1=第二个元素)否Lastelement
step每一次迭代的步长否1
var代表当前条目的变量名称否无
varStatus代表循环状态的变量名称否无
实例代码如下:
<c:setvalue="20"var="age"scope="request"></c:set>
<c:iftest="${requestScope.age>18}">成年了</c:if>

<c:iftest="${param.age>18}"var="isAdult"scope="request"></c:if>
isAdult:<c:outvalue="${requestScope.isAdult}"></c:out><br>

<c:choose>
<c:whentest="${param.age>60}">老年</c:when>
<c:whentest="${param.age>40}">中年</c:when>
<c:otherwise>青年</c:otherwise>
</c:choose>
<%
List<Customer>custs=newArrayList<Customer>();
custs.add(newCustomer(1,"AA"));
custs.add(newCustomer(2,"BB"));
custs.add(newCustomer(3,"CC"));
custs.add(newCustomer(4,"DD"));
custs.add(newCustomer(5,"EE"));
custs.add(newCustomer(6,"FF"));
request.setAttribute("custs",custs);
%>
<c:forEachitems="${requestScope.custs}"var="safly"begin="1"step="2"end="5">
${safly.id}:${safly.name}
</c:forEach>
<br>

<%
Map<String,Customer>custMap=newHashMap<String,Customer>();
custMap.put("a",newCustomer(1,"AA"));
custMap.put("b",newCustomer(2,"BB"));
custMap.put("c",newCustomer(3,"CC"));
custMap.put("d",newCustomer(4,"DD"));
custMap.put("e",newCustomer(5,"EE"));
request.setAttribute("custMap",custMap);
%>
<c:forEachitems="${requestScope.custMap}"var="safly">
${safly.key}---${safly.value.id}--${safly.value.name}<br>
</c:forEach>

<%
String[]names=newString[]{"A","B","C"};
request.setAttribute("names",names);
%>
<c:forEachvar="safly"items="${names}">
${safly}
</c:forEach>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java html js 前端