jsp-EL-JSTL标签库

本文详细介绍了JSP页面中的表达式语言(EL)用法,包括基本语法、不同作用域变量的获取方式及格式化标签的使用。同时,深入探讨了JSTL标签库的应用,涵盖核心标签、函数标签、数据库操作标签等,并提供了多个示例。

jsp el 表达式输出
表达式语言,必须在jsp页面中使用

${msg}
${st.name} 相当于 st.getName()
page pageScope
request requestScope
session sessionScope
application applicationScope

${2*3}
<hr>
<%
pageContext.setAttribute("age",18);
request.setAttribute("age",28);
session.setAttribute("age",38);
application.setAttribute("age",48);
%>
<hr>
${age}
<br>->pageScope -> requestScope -> sessionScope -> applicationScope <br>
${sessionScope.age}

pageContext.setAttribute("sts",new MyDbUtil().query("select * from student"));
pageContext.setAttribute("sss",new MyDbUtil().query("student","*","where 1=1","order by id desc","limit 20"));
<hr>
姓名:${sts[0].name},成绩:${sts[0].score}分,出生年月:<fmt:formatDate value="${sts[0].birthday}" pattern="yyyy年MM月dd日"/>
<hr>
${sss.size()}<br>
姓名:${sss[0].name}---<fmt:formatDate value="${sss[0].birthday}" pattern="yy-MM-dd"/>


${param.id} 相当于 <%=request.getParameter("id");%>


特别常用的EL

取得客户端的IP并显示 xxx.xxx.xxx.xxx
${pageContext.request.remoteAddr}

取得项目的路径 /web07
${pageContext.request.contextPath}

jsp jstl JSP标准标签库
jsp standard tag lib

引入标签库jar文件
javax.servlet.jsp.jstl.jar
jstl-impl.jar

必须在jsp页面上编写标签
核心 必须掌握
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach begin="1" end="10" step="1" var="c">
<c:if test="${(11-c)%2==0}">
<h3 style="background-color: #f00;">${11-c}</h3>
</c:if>
<c:if test="${(11-c)%2!=0}">
<h3 style="background-color: #ff0;">${11-c}</h3>
</c:if>

</c:forEach>
<hr>
<c:forEach begin="1" end="10" step="1" var="i">
<h3><fmt:formatNumber minIntegerDigits="3" pattern="0.000" value="${i}"/></h3>
</c:forEach>

<c:forEach begin="1" end="10" step="1" var="c">
<c:if test="${(11-c)%2==0}"><c:set var="cc" value="red"/> </c:if>
<c:if test="${(11-c)%2!=0}"><c:set var="cc" value="blue"/> </c:if>
<h3 style="background-color:${cc};">${11-c}</h3>
</c:forEach>

函数
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/functions" %>

数据库操作
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

xml技术操作 用量较少
<%@ taglib prefix="xml" uri="http://java.sun.com/jsp/jstl/xml" %>

格式化标签库 掌握
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber minIntegerDigits="3" pattern="0.000" value="${i}"/>
<fmt:formatDate value="${st.birthday}" pattern="MM月dd日"/>

转载于:https://www.cnblogs.com/Mkady/p/7001715.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值