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

jsp用到的jstl标签语言库之core标签库(一)

2017-09-19 00:33 316 查看
el语言${}在jsp中是默认使用的,

没想到jstl的标签在jsp中竟然不能使用,需要导入jstl.jar和standard.jar之后才能识别并使用。

如果开发中使用到jsp,标签语言是必须用到的,因为除了静态的html内容,其他的要是从后台数据库取出来的数据,一般是需要转化和判断的。

在standard.jar中有c.tld文件;

这应该是<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>也就是核心标签库中的标签定义。

引入核心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


不知道跟jsp自带的标签功能会不会有重叠的,不过一般喜欢使用jstl。

先来看最常用的

if

标签定义吧:

<tag>
<description>
Simple conditional tag, which evalutes its body if the
supplied condition is true and optionally exposes a Boolean
scripting variable representing the evaluation of this condition
</description>
<name>if</name>
<tag-class>org.apache.taglibs.standard.tag.rt.core.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>boolean</type>
</attribute>
<attribute>
<name>var</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>


丛上到下的意思是:

description:描述,英文不会翻译。

name:标签的名字,可以通过使用

attribute:标签能用的属性,包括test\var\scope.

test 就是判断的条件,

var 就是判断之后的结果,真或者假,var的值就是用来存放这个结果的,之后可以通过这个值取到这个结果

比如var=“test”,那就可以${name}取到test的结果

scope 作用域,jsp有四大作用域,request,session、、、、,随便放一个:scope=“request”

还有<.required>false<./required>就是不是必须的意思

<.rtexprvalue>false<./rtexprvalue>表示只能使用属性,不能用表达式

if的开始和结束标签之间的内容会根据test的真假来决定是否展示出来

下一个:

out

标签定义

<tag>
<description>
Like <%= ... >, but for expressions.
</description>
<name>out</name>
<tag-class>org.apache.taglibs.standard.tag.rt.core.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribu
ca3f
te>
<attribute>
<name>default</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>escapeXml</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>


value 必须的,一般它的值都是el表达式,不然就是静态的内容。

default 默认值,如果value取不到值,就显示默认的,

escapeXml 指定内容是否以纯文本显示,默认true、如果有这个属性也就是不忽略xml标签,就会展示HTML文本。

out标签之间放内容不会显示。

好了,全部内容已经讲完!

补充:

如果不想使用if==和if!=来判断(因为jstl没有else,只能用反向if判断咯,无奈~),

那就试试

choose

标签把,就是选一个的意思:

<c:choose>      本身只当做<c:when>和<c:otherwise>的父标签
<c:when>        <c:choose>的子标签,用来判断条件是否成立
<c:otherwise>   <c:choose>的子标签,接在<c:when>标签后,当<c:when>标签判断为false时被执行


写法:

<c:choose>
<c:when test="${1==1}" >输出1=2</c:when>
<c:otherwise >否则1!=2</c:otherwise>
</c:choose>


注意:

1、使用c:choose标签,就必须有c:when

2、可以不要c:otherwise,但是最多只能有一个c:otherwise标签,而且必须放到c:when 后面

3、c:when 中的test属性必需,而且判断必需写在${}中。

4、正常使用就不会错

还有一个非常重要的循环:
<c:forEach>    基础迭代标签,接受多种集合类型


forEach

循环必定会用到el语言${},因为肯定有变量,不是常量。

使用形式:

<c:forEach items="${list }" var="obj" varStatus="v">
<c:out value="${obj.name }"></c:out>
<c:out value="${obj[0] }"></c:out>

index:  ${v.index } //索引从0开始
count:  ${v.count } //计数从1开始
isFirst:${v.first } //是否循环的第一个
isLast: ${v.last }  //是否最后一个
currentObj:${v.current } //当前循环到的元素
</c:forEach>


标签定义说明:

<tag>
<description>
The basic iteration tag, accepting many different
collection types and supporting subsetting and other
functionality
</description>
<name>forEach</name>
<tag-class>org.apache.taglibs.standard.tag.rt.core.ForEachTag</tag-class>
<tei-class>org.apache.taglibs.standard.tei.ForEachTEI</tei-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Object</type>
</attribute>
<attribute>
<name>begin</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
<attribute>
<name>end</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
<attribute>
<name>step</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
<attribute>
<name>var</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>varStatus</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>


属性说明:

items 必须的,循环必须给list,哪怕为空

var 循环获取到的list集合中的当前一个元素

varStatus 当前状态,它的参数包括

current:当前这次迭代的(集合中的)项

index:迭代的下标,也就是集合的下标

count:循环计数。从1开始。

first:用来表明当前这轮迭代是否为第一次迭代的标志,返回true/false

last:用来表明当前这轮迭代是否为最后一次迭代的标志,返回true/false


另外还有begin,end,step,能够设定从集合的begin下标开始迭代,迭代到end下标(包括这个)结束,step表示相邻两次迭代的下标差值,默认为也就是依次迭代,可以设置跳跃迭代。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐