JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】(修订版)

 
 

前言

只有光头才能变强。

文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y

什么是JSTL

JSTL全称为 JSP Standard Tag Library 即JSP标准标签库

JSTL作为最基本的标签库,提供了一系列的JSP标签,实现了基本的功能:集合的遍历、数据的输出、字符串的处理、数据的格式化等等!

为什么要使用JSTL

使用JSTL标签库步骤:

  1. 导入jstl.jar和standard.jar开发包

  2. 在JSP页面中用tablib指令引入需要用到的JSTL标签

core标签库

`c:out`

640?wx_fmt=png
<%    session.setAttribute("name", "zhongfucheng");%>//<c:out/>标签支持标签体,default属性上的数据可以写在标签体中//<c:out value="${name}" escapeXml="true">您要的数据找不着</c:out><c:out value="${name}" default="您要的数据找不着" escapeXml="true"/>"name""zhongfucheng");
%>

//<c:out/>标签支持标签体,default属性上的数据可以写在标签体中
//<c:out value="${name}" escapeXml="true">您要的数据找不着</c:out>

<c:out value="${name}" default="您要的数据找不着" escapeXml="true"/>

`c:set`

640?wx_fmt=png

使用var属性

<c:set var="name" value="fucheng" scope="page"/>${name}"fucheng" scope="page"/>

${name}
640?wx_fmt=png
<c:set var="name" scope="page">    zhongfucheng</c:set>"page">
    zhongfucheng
</c:set>
<%--由于下面变量需要做加法运算,所以要定义出来,不然服务器是不知道我的变量是Integer类型的--%><%    Integer sessionCount = 0;    Integer applicationCount = 0;%><c:set var="sessionCount" value="${sessionCount+1}" scope="session"/><c:set var="applicationCount" value="${applicationCount+1}" scope="application"/>
    Integer sessionCount = 0;
    Integer applicationCount = 0;
%>
<c:set var="sessionCount" value="${sessionCount+1}" scope="session"/>

<c:set var="applicationCount" value="${applicationCount+1}" scope="application"/>
640?wx_fmt=png

使用target属性

<%--创建出JavaBean对象,设置为session范围的属性--%><jsp:useBean id="person" class="domain.Person" scope="session"/><%--获取到person对象,设置age属性的值为32--%><c:set target="${person}" property="age" value="32"/>${person.age}"person" class="domain.Person" scope="session"/>

<%--获取到person对象,设置age属性的值为32--%>
<c:set target="${person}" property="age" value="32"/>

${person.age}
640?wx_fmt=png

`c:remove`

remove标签就相当简单了,只有var和scope属性,代表的是删除域范围的属性

<%--创建出JavaBean对象,设置为session范围的属性--%><jsp:useBean id="person" class="domain.Person" scope="session"/><%--获取到person对象,设置age属性的值为32--%><c:set target="${person}" property="age" value="32"/>${person.age}<br><%--删除session属性--%><c:remove var="person" scope="session"></c:remove>${person.age==null?"存在session的person对象被删除了!":"我还在呢!"}"person" class="domain.Person" scope="session"/>

<%--获取到person对象,设置age属性的值为32--%>
<c:set target="${person}" property="age" value="32"/>

${person.age}
<br>

<%--删除session属性--%>
<c:remove var="person" scope="session"></c:remove>
${person.age==null?"存在session的person对象被删除了!":"我还在呢!"}
640?wx_fmt=png

`c:catch`

该标签主要用来处理程序中产生的异常。

catch标签也十分简单,只有一个var属性,var属性封装了异常的信息!

<%--创建出JavaBean对象,设置为session范围的属性--%><jsp:useBean id="person" class="domain.Person" scope="session"/><c:catch var="message">    <%--target属性只能是EL表达式,现在我是字符串,获取不到对象,肯定会抛出异常的!--%>    <c:set target="person" property="age" value="32"/></c:catch>${message}"person" class="domain.Person" scope="session"/>

<c:catch var="message">

    <%--target属性只能是EL表达式,现在我是字符串,获取不到对象,肯定会抛出异常的!--%>
    <c:set target="person" property="age" value="32"/>

</c:catch>

${message}
640?wx_fmt=png

`c:if`

640?wx_fmt=png

JSTL提供了if标签完成分支语句的实现,test属性是不可或缺的

var和scope属性我看来好像没什么用的(保存执行结果有什么用?)

<%--如果带过来的名字是zhongfucheng,那么可以登陆--%><c:if test="${param.name=='zhongfucheng'}">    用户名:<input type="text" name="username"><br>    密码:<input type="password" name="password"><br>    <input type="submit" value="登陆"></c:if><%--如果带过来的名字是ouzicheng,那么就是注册--%><c:if test="${param.name=='ouzicheng'}">    用户名:<input type="text" name="username"><br>    密码:<input type="password" name="password"><br>    <input type="submit" value="注册"></c:if>if test="${param.name=='zhongfucheng'}">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="登陆">
</c:if>

<%--如果带过来的名字是ouzicheng,那么就是注册--%>
<c:if test="${param.name=='ouzicheng'}">
    用户名:<input type="text" name="username"><br>
    密码:<input type="password" name="password"><br>
    <input type="submit" value="注册">
</c:if>
640?wx_fmt=png
640?wx_fmt=png

`c:choose`

if标签没有else的功能,如果需要类似于java中的if else流程就需要使用choose标签。

choose标签需要联合when和otherwise标签一起使用!

<c:choose>    <c:when test="${param.name=='zhongfucheng'}">        你好啊,zhongfucheng    </c:when>    <c:when test="${param.name=='ouzicheng'}">        你好啊,ouzicheng    </c:when>    <c:otherwise>        你是谁啊?别随便过来!    </c:otherwise></c:choose>"${param.name=='zhongfucheng'}">
        你好啊,zhongfucheng
    </c:when>
    <c:when test="${param.name=='ouzicheng'}">
        你好啊,ouzicheng
    </c:when>
    <c:otherwise>
        你是谁啊?别随便过来!
    </c:otherwise>
</c:choose>
640?wx_fmt=png
640?wx_fmt=png

`c:forEach`

640?wx_fmt=png

forEach为循环标签,相当于Java中的while和for

<%    List list = new ArrayList<>();    list.add("zhongfucheng");    list.add("ouzicheng");    list.add("xiaoming");    session.setAttribute("list", list);%>new ArrayList<>();
    list.add("zhongfucheng");
    list.add("ouzicheng");
    list.add("xiaoming");

    session.setAttribute("list", list);
%>
<c:forEach  var="list" items="${list}" >    ${list}<br></c:forEach>"${list}" >
    ${list}<br>
</c:forEach>
640?wx_fmt=png
<%    Map map = new HashMap();    map.put("1", "zhongfucheng");    map.put("2", "xiaohong");    map.put("3", "xiaoming");    session.setAttribute("map",map);%><c:forEach  var="me" items="${map}" >    ${me.key}  ${me.value}<br></c:forEach>new HashMap();
    map.put("1""zhongfucheng");
    map.put("2""xiaohong");
    map.put("3""xiaoming");

    session.setAttribute("map",map);
%>

<c:forEach  var="me" items="${map}" >

    ${me.key}  ${me.value}<br>

</c:forEach>
640?wx_fmt=png
<c:forEach  var="list" items="${list}" varStatus="varStatus" >    ${list}您的下标是:${varStatus.index}<br></c:forEach>"${list}" varStatus="varStatus" >

    ${list}您的下标是:${varStatus.index}<br>

</c:forEach>
640?wx_fmt=png

`c:forTokens`

该标签类似于String类的split()和for循环的一种集合

它与forEach标签非常相似,都有begin、end、step、items、var、varStatus属性,不同的是forTokens标签的items属性里面是字符串,这个字符串会被delims属性的内容分割成多个字符串!

<c:forTokens items="zhongfucheng,ouzicheng,xiaoming,xiaohong" var="name" delims="," >    ${name}</c:forTokens>"name" delims="," >
    ${name}
</c:forTokens>
640?wx_fmt=png

`c:import`

import标签类似于JSP行为<jsp:include/>和JSP指令<%include>

import标签的属性:

  1. url【指定要包含的路径,Internet所有的url都可以】

  2. context【访问同一个web容器的其他资源,以"/"开头】

  3. var【保存导入的文件的内容,以String类型存储】

  4. socpe【保存的范围,默认是page】

  5. charEncoding【字符编码】

  6. varReader【保存导入文件的内容,以Reader类型存储】

当然了,import标签功能更加更大!强大在哪里呢?import标签可以引入Internet网页上的内容,也就是说,csdn也可以引入进来!

<c:import url="http://www.youkuaiyun.com" charEncoding="UTF-8" />"http://www.youkuaiyun.com" charEncoding="UTF-8" />
640?wx_fmt=png
<c:import url="http://www.youkuaiyun.com" charEncoding="UTF-8" var="net"/>优快云的源码是:<br><br><br><br><br><c:out value="${net}" escapeXml="true"></c:out>"http://www.youkuaiyun.com" charEncoding="UTF-8" var="net"/>

优快云的源码是:<br><br><br><br><br>
<c:out value="${net}" escapeXml="true"></c:out>
640?wx_fmt=png

`c:param`


`c:url`

url标签十分实用!在浏览器禁用Cookie的时候,我们之前学Servlet时解决办法是:response.encodeURL()。url标签也可以实现这样的功能,再配合param标签使用,就十分实用了!

640?wx_fmt=png
<c:url value="2.jsp" var="url">    <c:param name="name" value="中国!">    </c:param></c:url><a href="${url}">我经过了URL地址重写!</a>"url">
    <c:param name="name" value="中国!">
    </c:param>
</c:url>

<a href="${url}">我经过了URL地址重写!</a>
640?wx_fmt=png

`c:redirect`

redirect标签用于实现Redirect功能,当然了,此标签也能够配合param标签使用!

640?wx_fmt=png
<c:redirect url="2.jsp" >    <c:param name="name" value="zhongfucheng">    </c:param></c:redirect>
    <c:param name="name" value="zhongfucheng">
    </c:param>
</c:redirect>
640?wx_fmt=png

fmt标签库

fmt标签库也叫做国际化标签库。这里就不详细说明了,等我讲到Web 国际化的时候才讲吧!

fn方法库

fn方法库也叫做EL函数库、fn标签库。这个在讲解EL表达式的时候有详细的说明,可转移到我EL表达式的博文中

最后

乐于输出干货的Java技术公众号:Java3y。公众号内有200多篇原创技术文章、海量视频资源、精美脑图,不妨来关注一下!

640?wx_fmt=jpeg

有帮助?好看!转发! 640



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值