JSTL(JSP标准标签库)

JSTL

在这里插入图片描述
使用JSTL首先要下载两个包taglibs-standard-impl-1.2.5.jar taglibs-standaed-spec-1.2.5.jar

还要必须使用taglib指令,taglib指令的作用是声明JSP文件使用的标签库,同时引入该标签库
语法:

<%@ taglib prefix=''c" uri="http://java.sun.com/jsp/jstl/core"%>
//声明核心标签库,prefix表示前缀

用核心标签进行基本数据操作

1.<c:out>

用于向页面输出值,等价于<%= %>

语法:
格式1:
<c:out value=''value'' [excapeXml="{true|false}"] defalut="dafaultValue" />
格式2:

<c:out value=''value'' [excapeXml="{true|false}"] >
	defalut value
</c:out>

value可以是EL表达式
excapeXml 用于遇到特殊标记(< > &)指定是否需要转义 默认为true
defalut属性表示value的值为null或不存在,则输出默认值
示例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
  session.setAttribute("msg","这是<c:out>示例");
%>
<c:out value="${msg}"></c:out>
</body>
</html>

2.<c:set>

定义一个字符串类型的作用域变量 并通过变量名引用它,还可以重新设置作用域变量的属性值

包含属性:value target property var scope
var指定作用域变量名
value指定作用域变量值
格式1:
<c:set var = "varName" value ="value" [scope={page|request|session|application}]>
格式2:

<c:set var="varName" [scope={page|request|session|application}]>
	value
</c:set>

示例:

<c:set value="欢迎" scope="session" var="msg"></c:set>
<c:out value=":${msg}"></c:out>

表示将"字符串"欢迎存入session 起名为msg 然后显示

3.<c:remove>

删除存在于scope中的变量

示例:

<%
session.setAttribute("msg","欢迎");
%>
<c:remove var="msg" scope="session" />

表示将session中的msg移除

用核心标签进行流程控制

1.<c:if>
用于简单的条件语句
基本语法:

<c:if test="${判断条件}">
...
</c:if>
<c:if test="${判断条件}">
...
</c:if>

示例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>c:if页面</title>
</head>
<body>
<%
    session.setAttribute("score",5);
%>
<c:if test="${score >= 60}">及格</c:if>
<c:if test="${score < 60}">不及格</c:if>
</body>
</html>

2.<c:choose> <c:when> <c:otherwise>
类似于if- else if-else作用

示例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>c:choose c:when c:otherwise</title>
</head>
<body>
<%
    session.setAttribute("score",5);
%>
<c:choose>
    <c:when test="${score>=80}">优秀</c:when>
    <c:when test="${score>=60 && score<80}">及格</c:when>
    <c:otherwise>不及格</c:otherwise>
</c:choose>
</body>
</html>

结果为不及格

3.<c:forEach>
循环控制标签
语法:

<c:forEach var="元素名" items="集合名"></c:forEach>
可选属性: begin="起始" end="结束" step="步长"

var 类似于临时变量
items 类似于集合
示例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>c:forEach</title>
</head>
<body>
<%
    ArrayList al = new ArrayList();
    al.add("陈丽");
    al.add("陈强");
    al.add("陈林");
    session.setAttribute("students",al);
%>
<c:forEach items="${students}" var="student">
    ${student}
</c:forEach>
</body>
</html>

4.<c:forTokens>
用于浏览字符串中所有的成员,由指定分隔符分开

语法:

<c:forTokens items="字符串" delims="分隔符" var="临时子串">${临时子串}</c:forTokens>
//可选属性:  begin="起始" end="结束" step="步长"

XML标签库

标签的基本功能:
<x:parse> 解析XML文件
<x:out> 在<x:parse>解析后保存的变量中取得指定的XML文件内容,并显示在页面上
<x:set>将某个XML文件中的元素的实体内容或属性保存到变量中
<x:if> 由XPath的判断得到结果,根据情况决定是否显示其标签所包含的内容
<x:choose> <x:when> <x:oterwise> 参考<c:choose> <c:when> <c:otherwise>
<x:forEach>对XML文件元素进行循环控制

函数标签库

标签的基本功能:
fn:length计算传入对象的长度 该对象为集合或者String类型
fn:contains 判断源字符串是否包含某个子串 语法: ${fn:contains("源字符串","子串")}
fn:containIgnoreCasefn:contains标签作用类似,区别在于忽略字符串大小写
fn:startWith 判断字符串是否是指定字符串开始 语法: ${fn:startWith("源字符串","子串")}
fn:endsWith判断字符串是否是指定字符串结尾
fn:escapeXml 将所有特殊字符串转化为字符实体码 语法:${fn:escapeXml(特殊字符)}
fn:indexOf 得到子串于源字符串匹配的起始位置
fn:join 将字符串数组中的每个字符串加上分隔符 语法:${fn:join(数组,"分隔符")}
fn:replace 为源字符串做替代工作${fn:replace("源字符串","被替换字符串","替换字符串")}
fn:splitfn:join 作用相反
fn:substring 截取字符串
fn:substringAfter截取字符串,从指定位置截取到末尾
fn:substringBefore截取字符串,从开始截取到指定位置
fn:toLowerCase字符串转小写
fn:toUpperCase字符串转大写
fn:trim去除字符串两边空格

发布了103 篇原创文章 · 获赞 5 · 访问量 2236

猜你喜欢

转载自blog.csdn.net/weixin_42452726/article/details/102907328
今日推荐