ESTL表达式(一)

网友投稿 632 2022-10-20

ESTL表达式(一)

ESTL表达式(一)

/**作者:呆萌老师*☑csdn认证讲师*☑高级讲师*☑腾讯课堂认证讲师*☑网易云课堂认证讲师*☑华为开发者学堂认证讲师*☑爱奇艺千人名师计划成员*在这里给大家分享技术、知识和生活*各种干货,记得关注哦!*/

1 什么是jsTL

JSTL是apache对EL表达式的扩展(也就是说JSTL依赖EL),JSTL是标签语言!JSTL标签使用以来非常方便,它与JSP动作标签一定,只不过它不是JSP内置的标签,需要我们自己导包,以及指定标签库而已!

如果你使用MyEclipse开发JavaWeb,那么在把项目发布到Tomcat时,你会发现,MyEclipse会在lib目录下存放jstl的Jar包!如果你没有使用MyEclipse开发那么需要自己来导入这个JSTL的Jar包:jstl-1.2.jar。

2 JSTL标签库

JSTL一共包含四大标签库:

core:核心标签库,我们学习的重点;fmt:格式化标签库,只需要学习两个标签即可;sql:数据库标签库,不需要学习了,它过时了;xml:xml标签库,不需要学习了,它过时了。

3 使用taglib指令导入标签库

除了JSP动作标签外,使用其他第三方的标签库都需要:

导包;在使用标签的JSP页面中使用taglib指令导入标签库;

下面是导入JSTL的core标签库:

<%@ taglib prefix="c" uri="%>

prefix="c":指定标签库的前缀,这个前缀可以随便给值,但大家都会在使用core标签库时指定前缀为c;uri="class="data-table" data-id="t7a7e9d1-QhWZIYaf" data-transient-attributes="class" data-width="956px" style="width: 100%; outline: none; border-collapse: collapse;">

<c:out value=”aaa”/>

输出aaa字符串常量

<c:out value=”${aaa}”/>

与${aaa}相同

<c:out value=”${aaa}” default=”xxx”/>

当${aaa}不存在时,输出xxx字符串

<%

request.setAttribute("a","<script>alert('hello');</script>");

%>

<c:out value="${a }" default="xxx" escapeXml="false" />

当escapeXml为false,不会转换“<”、“>”。这可能会受到JavaScript gongji。

set

<c:set var=”a” value=”hello”/>

在pageContext中添加name为a,value为hello的数据。

<c:set var=”a” value=”hello” scope=”session”/>

在session中添加name为a,value为hello的数据。

4.2 remove

         <%

                  pageContext.setAttribute("a", "pageContext");

                  request.setAttribute("a", "session");

                  session.setAttribute("a", "session");

                  application.setAttribute("a", "application");

         %>

         <c:remove var="a"/>

         <c:out value="${a }" default="none"/>

删除所有域中name为a的数据!

<c:remove var="a" scope=”page”/>

删除pageContext中name为a的数据!

4.3 url

url标签会在需要URL重写时添加sessionId。

<c:url value="/"/>

输出上下文路径:/day08_01/

<c:url value="/" var="a" scope="request"/>

把本该输出的结果赋给变量a。范围为request

<c:url value="/AServlet"/>

输出:/day08_01/AServlet

<c:url value="/AServlet">

<c:param name="username" value="abc"/>

<c:param name="password" value="123"/>

</c:url>

输出:/day08_01/AServlet?username=abc&password=123

如果参数中包含中文,那么会自动使用URL编码!

4.4 if

if标签的test属性必须是一个boolean类型的值,如果test的值为true,那么执行if标签的内容,否则不执行。

<c:setvar="a"value="hello"/>

<c:iftest="${not empty a }">

    <c:outvalue="${a }"/>

</c:if>


 比较字符串用’’

   <c:iftest="${uname=='123'}">

            ok

   </c:if>

4.5 choose

choose标签对应Java中的if/else if/else结构。when标签的test为true时,会执行这个when的内容。当所有when标签的test都为false时,才会执行otherwise标签的内容。

<c:setvar="score"value="${param.score }"/>

<c:choose>

    <c:whentest="${score > 100 || score < 0}">错误的分数:${score }</c:when>

    <c:whentest="${score >= 90 }">A</c:when>

    <c:whentest="${score >= 80 }">B</c:when>

    <c:whentest="${score >= 70 }">C</c:when>

    <c:whentest="${score >= 60 }">D</c:when>

    <c:otherwise>E</c:otherwise>

</c:choose>

4.6 forEach

forEach当前就是循环标签了,forEach标签有多种两种使用方式:

使用循环变量,指定开始和结束值,类似for(int i = 1; i <= 10; i++) {};循环遍历集合,类似for(Object o : 集合);

循环变量方式:

<c:setvar="sum"value="0"/>

<c:forEachvar="i"begin="1"end="10">

    <c:setvar="sum"value="${sum + i}" />

</c:forEach>

<c:outvalue="sum = ${sum }"/>

<c:setvar="sum"value="0"/>

<c:forEachvar="i"begin="1"end="10"step="2">

    <c:setvar="sum"value="${sum + i}" />

</c:forEach>

<c:outvalue="sum = ${sum }"/>

遍历集合或数组方式:

<%

String[] names = {"zhangSan", "liSi", "wangWu", "zhaoLiu"};

pageContext.setAttribute("ns", names);

%>

<c:forEachvar="item"items="${ns }">

    <c:outvalue="name: ${item }"/><br/>

</c:forEach>

遍历List

<%

    List<String> names = new ArrayList<String>();

    names.add("zhangSan");

    names.add("liSi");

    names.add("wangWu");

    names.add("zhaoLiu");

    pageContext.setAttribute("ns", names);

%>

<c:forEachvar="item"items="${ns }">

    <c:outvalue="name: ${item }"/><br/>

</c:forEach>

遍历Map

<%

    Map<String,String> stu = new LinkedHashMap<String,String>();

    stu.put("number", "N_1001");

    stu.put("name", "zhangSan");

    stu.put("age", "23");

    stu.put("sex", "male");

    pageContext.setAttribute("stu", stu);

%>

<c:forEachvar="item"items="${stu }">

    <c:outvalue="${item.key }: ${item.value }"/><br/>

</c:forEach>

forEach标签还有一个属性:varStatus,这个属性用来指定接收“循环状态”的变量名,例如:,这时就可以使用vs这个变量来获取循环的状态了。

count:int类型,当前以遍历元素的个数;index:int类型,当前元素的下标;first:boolean类型,是否为第一个元素;last:boolean类型,是否为最后一个元素;current:Object类型,表示当前项目。

<c:forEachvar="item"items="${ns }" varStatus="vs">

    <c:iftest="${vs.first }">第一行:</c:if>

    <c:iftest="${vs.last }">最后一行:</c:if>

    <c:outvalue="${vs.count }: "/>

    <c:outvalue="[${vs.index }]: "/>

    <c:outvalue="name: ${vs.current }"/><br/>

</c:forEach>

5 fmt标签库常用标签

fmt标签库是用来格式化输出的,通常需要格式化的有时间和数字。

格式化时间:

<%@taglibprefix="fmt"uri="style="color: #bf5f3f;">%>

......

<%

    Date date = new Date();

    pageContext.setAttribute("d", date);

%>

<fmt:formatDatevalue="${d }" pattern="yyyy-MM-dd HH:mm:ss"/>

格式化数字:

<%

    double d1 = 3.5;

    double d2 = 4.4;

    pageContext.setAttribute("d1", d1);

    pageContext.setAttribute("d2", d2);

%>

<fmt:formatNumbervalue="${d1 }" pattern="0.00"/><br/>

<fmt:formatNumbervalue="${d2 }" pattern="#.##"/>

更多了解

​​https://edu./course/20519.html​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:.Net简单的微ORM框架:NPoco
下一篇:Mobly- 移动设备测试框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~