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