JSTL标签库

62 篇文章 3 订阅

JSTL标签库

下载JSTL标签库

官网地址:http://tomcat.apache.org/

JSTL v1.2.5组件介绍

作用域对象描述
taglibs-standard-spec-1.2.5.jar标签库定义包(必须)
taglibs-standard-impl-1.2.5.jar标签库实现包(必须)
taglibs-standard-jstlel-1.2.5.jarEL表达式支持包(备选)
taglibs-standard-compat-1.2.5.jar1.0版本兼容包(备选)

进入官网首页找到Apache Standard Taglib 1.2.5 Released栏,download进入详细页面在最下方下载

安装JSTL标签库

JSTL有两种安装方式

  • 将Jar文件复制到工程的/WEB-INF/lib目录(推荐)
  • 将Jar文件复制到Tomcat安装目录的lib(全局组件依赖目录)目录(全局工程项目生效)

在Tomcat的lib目录中可以看到好多jar包文件:

servlet-api.jar servlet的实现

jsp-api.jar jsp的实现

el-api.jar el表达式的实现

还有一些tomcat运行相关的jar包,tomcat-i18n-es.jar国际化jar包等等

JSTL核心库

JSTL的标签库种类

JSTL按功能划分可划分为五类标签库

类别
核心标签库 - core
格式化输出标签库 - fmt
SQL操作变迁库 - sql
XML操作标签库 - xml
函数标签库 - functions

后3个现在基本不用了,因为在java中有了更好的方案来替代这种方式

引用JSTL核心库

  • 核标签库(Core)是JSTL最重要的标签库,提供了JSTL的基础功能
  • 在JSP页面顶端声明taglib指令引用,使用prefix="c"为核心标签库(core)声明一个前缀:<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  • JSTL核心标签库在taglibs-standard-impl.jar由META-INF/c.tld定义

小技巧:

选中ISO-8859-1按ctrl+F,replaceAll替换UTF-8

alt+/弹出智能提示,选择要引用的路径

JSTL判断标签

JSTL核心库提供了两组判断的标签

  • <c:if> - 单分支标签
  • <c:choose>、<c:when>、<c:otherwise> - 多分支判断

例子:

jstl.JstlServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.setAttribute("score", 99);
	request.setAttribute("grade", "A");
	request.getRequestDispatcher("/core.jsp").forward(request, response);
}

core.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	
	<h1>${requestScope.score }</h1>
	
	<c:if test="${score>=60 }">
		<h1 style="color:green">恭喜!</h1>
	</c:if>
		<c:if test="${score<60 }">
		<h1 style="color:red">菜逼!</h1>
	</c:if>
	
	<!-- choose when otherwise -->
	${grade }
	<c:choose>
		<c:when test="${grade=='A'}">
			<h2>基本操作</h2>
		</c:when>
		<c:when test="${grade=='B'}">
			<h2>有点菜</h2>
		</c:when>
		<c:when test="${grade=='C'}">
			<h2>辣鸡</h2>
		</c:when>
		<c:when test="${grade=='D'}">
			<h2>校霸</h2>
		</c:when>
		<c:otherwise>
			<h2>大神</h2>
		</c:otherwise>
	</c:choose>
	
</body>
</html>

JSTL集合遍历

<c:forEach>标签用于遍历集合(Collection)中的每一个对象

<c:forEach var="p" items="${persons}" varStauts="idx">
    第${idx.index+1}位<br/>
    姓名:${p.name} 性别:${p.sex} 年龄:${p.age}
</c.forEach>

fmt(format)格式化标签库

  • uri:http://java.sun.com/jsp/jstl/fmt
  • <fmt:formatDate value=“” pattern=“”> 格式化日期标签
  • <fmt:formatNumber value=“” pattern=“”> 格式化数字标签

out标签

  • out标签可以用于设置默认值,和让html按照原格式输出
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setAttribute("amt", 19874454.154);
		request.setAttribute("now", new java.util.Date());
		request.setAttribute("html", "<a href='index.html'>index</a>");
		request.setAttribute("nothing", null);
	%>
	<fmt:formatDate value="${requestScope.now }" pattern="yyyy年MM月dd日HH时mm分ss秒SSS毫秒"></fmt:formatDate>
	<br/>
	<p><fmt:formatNumber value="${amt }" pattern="0.00"></fmt:formatNumber></p>
	<p>¥<fmt:formatNumber value="${amt }" pattern="0,000.00"></fmt:formatNumber>元</p>
	<p>¥<fmt:formatNumber value="${amt }" pattern="0,00.00"></fmt:formatNumber>元</p>z
	<!-- 设置默认值和是否转移xml -->
	<h2>null默认值:<c:out value="${nothing }" default="无"></c:out></h2>
	<h2><c:out value="${html }" escapeXml="true"></c:out></h2>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

摘星喵Pro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值