EL=Expression Language替代JSP页面中的复杂代码
变量属性范围名称
pageScope
requestScope
sessionScope
applicationScope
${user.name}==${user["name"]}
${sessionScope.user[1]}
JSTL=JavaServerPages Standard Tag Library,JSP标准标签库,实现JSP页面中逻辑处理
选择JSTL支持,
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>通用标签、条件标签、迭代标签、URL标签
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>字符串处理函数
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>格式化数据和日期
通用标签
set:设置指定范围内的变量值
out:计算表达式并将结果输出显示
remove:删除指定范围内的变量
<c:set var="example" value="{100+1}" scope="session"></c:set>
借助<jsp:userBean>设置JavaBeans的属性值
<c:set value="赵五" target="${person}" property="name"></c:set>
<c:out value="${example}" default="null"/>
<c:remove var="example" scope="session"/>
<c:catch var="exception">
容易产生异常的代码,异常信息存储在exception变量中
</c:catch>
条件标签
<c:if test="codition" var="name" scope="applicationArea">
</c:if>
其中<c:otherwise>只能使用一次
<c:choose>
<c:when test="${pageScope.score>=90}">优秀</c:when>
<c:when test="${pageScope.score<90 && pageScope.score>=70}">良好</c:when>
<c:when test="${pageScope.score<70 && pageScope.score>=60}">及格</c:when>
<c:otherwise>不及格</c:otherwise>
</c:choose>
迭代标签
<c:forEach var="product" items="${requestScope.products}" varStatus="status">
<!-- 如果是偶数行,为该行换背景颜色 -->
<tr <c:if test="${status.index % 2 == 1 }">style="background-color:rgb(219,241,212);"</c:if>>
<td>${product.name }</td>
<td>${product.area }</td>
<td>${product.price }</td>
</tr>
</c:forEach>
<c:forEach var="varName" varStatus="vatStatusName" begin="start" end="end" step="count">
…循环体代码…
</forEach>
<c:forEach items=collection var=name begin=start end=end step=count>
循环体代码
</forEach>
<c:forTokens>用于浏览字符串,并根据指定的字符串截取字符串
语法<c:forTokens items="stringOfTokens" delims="delimiters" [var="name" begin="begin" end="end" step="len" varStatus="statusName"]></c:forTokens>
var:String,迭代参数的名字
items:String,将要迭代的String
varStatus:String,表示迭代的状态,可以访问迭代自身的信息,index当前指到的成员的索引;count当前总共指到成员的总数;
first当前指到的成员是否为第一个成员;last当前指到的成员是否为最后一个成员
delims:String,分隔符
begin:int,如果指定items,那么就从items的index[begin]开始迭代;如果没有指定items,那么就从指定的index开始迭代,相当于for(int i=begin;;)语句。
end:int,如果指定items,那么就在items的index[end]结束迭代;如果没有指定items,那么就在指定的index结束,相当于for(;i<end;)语句。
step:int,迭代的步长
<c:import url="url" [var="varName"] [scope="{page|request|session|application}"] >
[<c:param name="paramName" value="paramValue"/>]
</c:import>
<c:import url="http://www.baidu.com" context="" charEncoding="utf-8" value="urlStr" scope="session"></c:import>
当<c:import>标签中未指定var变量时,会直接将包含进来的其他网页文件内容显示出来,如果指定了var变量,则会将内容存放到var变量中,不显示。
<c:redirect url=""><c:param name=""></c:param></c:redirect>重定向请求
<c:redirect url="http://localhost:8080/Jsp/jsp/login.jsp">
<c:param name="userName">zmczdd</c:param>
<c:param name="userPwd">zmczdd</c:param>
</c:redirect>
<c:url> 用于动态生成一个 String 类型的URL
<c:url value="value" [var="name"] [scope="..."] [context="context"]>
<c:param name="name1" value="value1">
</c:url>
<c:url value="value" [var="name"] [scope="..."] [context="context"]/>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
fn:contains(string, substring)
如果参数string中包含参数substring,返回true
fn:containsIgnoreCase(string, substring)
如果参数string中包含参数substring(忽略大小写),返回true
fn:endsWith(string, suffix)
如果参数string以参数suffix结尾,返回true
fn:escapeXml(string)
将有特殊意义的XML(和HTML)转换为对应的XML character entity code,并返回
fn:indexOf(string,substring)
返回参数substring在参数string中第一次出现的位置
fn:join(array,separator)
将一个给定的数组array用给定的间隔符separator串在一起,组成一个新的字符串并返回。
fn:length(item)
返回参数item中包含元素的数量。参数Item类型是数组、collection或者String。如果是String类型,返回值是String中的字符数。
fn:replace(string, before, after)
返回一个String对象。用参数after字符串替换参数string中所有出现参数before字符串的地方,并返回替换后的结果
fn:split(string, separator)
返回一个数组,以参数separator 为分割符分割参数string,分割后的每一部分就是数组的一个元素
fn:startsWith(string, prefix)
如果参数string以参数prefix开头,返回true
fn:substring(string, begin, end)
返回参数string部分字符串, 从参数begin开始到参数end位置,包括end位置的字符
fn:substringAfter(string, substring)
返回参数substring在参数string中后面的那一部分字符串
fn:substringBefore(string, substring)
返回参数substring在参数string中前面的那一部分字符串
fn:toLowerCase(string)
将参数string所有的字符变为小写,并将其返回
fn:toUpperCase(string)
将参数string所有的字符变为大写,并将其返回
fn:trim(string)
去除参数string 首尾的空格,并将其返回
用户设置语言环境
<fmt:setLocale value="expression" scope="scope" variant="expression"/>
设置时区
<fmt:setTimeZone value="expression" var="name" scope="scope"/>
用来格式化和显示日期和时间(数据输出)
<fmt:formatDate value="expression" timeZone="expression" type="time/date/both" dateStyle="default/short/medium/long/full"
timeStyle="style" var="name" pattern=" expression" scope="scope"/>
用来解析日期和时间值(数据输入)
<fmt:parseDate value="expression" type="field" dateStyle="style" timeStyle="style"
pattern="expression" var="name" timeZone=" expression" parseLocale="expression" scope="scope"/>
<fmt:parseDate type="field" dateStyle="style" timeStyle="style" pattern="expression" timeZone="expression"
var="name" parseLocale="expression" scope="scope">body content </fmt:parseDate>
<fmt:formatNumber value="expression" type="number/currency/percentage" pattern="expression"
currencyCode="expression" currencySymbol="expression" //货币的单位符号
maxIntegerDigits="expression" minIntegerDigits="expression"
maxFractionDigits="expression" minFractionDigits="expression" //小数点前后的位数
groupingUsed="true/false" var="name" scope="scope"/>
<fmt:parseNumber value="expression" type="type" pattern="expression"
parseLocale="expression" integerOnly="expression" var="name" scope="scope"/>
<fmt:parseNumber type="type" var="name" scope="scope" pattern="expression"
parseLocale="expression" integerOnly="expression"/>body content </fmt:parseNumber>
数据库
<sql:setDataSource dataSource="expression" var="name" scope="scope"/>
<sql:setDataSource url="expression" driver="expression" user="expression" password="expression"var="name" scope="scope"/>
查询
<sql:query var="" dataSource="">
sql语句
<sql:param value="${6}"/>
</sql:query>
<sql:query sql="expression" dataSource="expression" var="name" scope="scope" maxRows="expression" startRow="expression"/>
<sql:query sql="expression" dataSource="expression" var="name" scope="scope" maxRows="expression" startRow="expression">
<sql:param value="expression"/>
...
</sql:query>
<sql:query dataSource="expression" var="name" scope="scope" maxRows="expression" startRow="expression">
SQL statement
<sql:param value="expression"/>
...
</sql:query>
日期参数
<sql:dateParam value="expression" type="date/time/timestamp"/>
更新
<sql:update sql="expression" dataSource="expression" var="name" scope="scope"/>
<sql:update sql="expression" dataSource="expression" var="name" scope="scope">
<sql:param value="expression"/>
...
</sql:update>
<sql:update dataSource="expression" var="name" scope="scope">
SQL statement
<sql:param value="expression"/>
...
</sql:update>
事务
<sql:transaction dataSource=" expression" isolation="read_committed/read_uncommitted/repeatable_read/serializable">
<sql:query .../> or <sql:update .../>
</sql:transaction>
变量属性范围名称
pageScope
requestScope
sessionScope
applicationScope
${user.name}==${user["name"]}
${sessionScope.user[1]}
JSTL=JavaServerPages Standard Tag Library,JSP标准标签库,实现JSP页面中逻辑处理
选择JSTL支持,
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>通用标签、条件标签、迭代标签、URL标签
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>字符串处理函数
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>格式化数据和日期
通用标签
set:设置指定范围内的变量值
out:计算表达式并将结果输出显示
remove:删除指定范围内的变量
<c:set var="example" value="{100+1}" scope="session"></c:set>
借助<jsp:userBean>设置JavaBeans的属性值
<c:set value="赵五" target="${person}" property="name"></c:set>
<c:out value="${example}" default="null"/>
<c:remove var="example" scope="session"/>
<c:catch var="exception">
容易产生异常的代码,异常信息存储在exception变量中
</c:catch>
条件标签
<c:if test="codition" var="name" scope="applicationArea">
</c:if>
其中<c:otherwise>只能使用一次
<c:choose>
<c:when test="${pageScope.score>=90}">优秀</c:when>
<c:when test="${pageScope.score<90 && pageScope.score>=70}">良好</c:when>
<c:when test="${pageScope.score<70 && pageScope.score>=60}">及格</c:when>
<c:otherwise>不及格</c:otherwise>
</c:choose>
迭代标签
<c:forEach var="product" items="${requestScope.products}" varStatus="status">
<!-- 如果是偶数行,为该行换背景颜色 -->
<tr <c:if test="${status.index % 2 == 1 }">style="background-color:rgb(219,241,212);"</c:if>>
<td>${product.name }</td>
<td>${product.area }</td>
<td>${product.price }</td>
</tr>
</c:forEach>
<c:forEach var="varName" varStatus="vatStatusName" begin="start" end="end" step="count">
…循环体代码…
</forEach>
<c:forEach items=collection var=name begin=start end=end step=count>
循环体代码
</forEach>
<c:forTokens>用于浏览字符串,并根据指定的字符串截取字符串
语法<c:forTokens items="stringOfTokens" delims="delimiters" [var="name" begin="begin" end="end" step="len" varStatus="statusName"]></c:forTokens>
var:String,迭代参数的名字
items:String,将要迭代的String
varStatus:String,表示迭代的状态,可以访问迭代自身的信息,index当前指到的成员的索引;count当前总共指到成员的总数;
first当前指到的成员是否为第一个成员;last当前指到的成员是否为最后一个成员
delims:String,分隔符
begin:int,如果指定items,那么就从items的index[begin]开始迭代;如果没有指定items,那么就从指定的index开始迭代,相当于for(int i=begin;;)语句。
end:int,如果指定items,那么就在items的index[end]结束迭代;如果没有指定items,那么就在指定的index结束,相当于for(;i<end;)语句。
step:int,迭代的步长
<c:import url="url" [var="varName"] [scope="{page|request|session|application}"] >
[<c:param name="paramName" value="paramValue"/>]
</c:import>
<c:import url="http://www.baidu.com" context="" charEncoding="utf-8" value="urlStr" scope="session"></c:import>
当<c:import>标签中未指定var变量时,会直接将包含进来的其他网页文件内容显示出来,如果指定了var变量,则会将内容存放到var变量中,不显示。
<c:redirect url=""><c:param name=""></c:param></c:redirect>重定向请求
<c:redirect url="http://localhost:8080/Jsp/jsp/login.jsp">
<c:param name="userName">zmczdd</c:param>
<c:param name="userPwd">zmczdd</c:param>
</c:redirect>
<c:url> 用于动态生成一个 String 类型的URL
<c:url value="value" [var="name"] [scope="..."] [context="context"]>
<c:param name="name1" value="value1">
</c:url>
<c:url value="value" [var="name"] [scope="..."] [context="context"]/>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
fn:contains(string, substring)
如果参数string中包含参数substring,返回true
fn:containsIgnoreCase(string, substring)
如果参数string中包含参数substring(忽略大小写),返回true
fn:endsWith(string, suffix)
如果参数string以参数suffix结尾,返回true
fn:escapeXml(string)
将有特殊意义的XML(和HTML)转换为对应的XML character entity code,并返回
fn:indexOf(string,substring)
返回参数substring在参数string中第一次出现的位置
fn:join(array,separator)
将一个给定的数组array用给定的间隔符separator串在一起,组成一个新的字符串并返回。
fn:length(item)
返回参数item中包含元素的数量。参数Item类型是数组、collection或者String。如果是String类型,返回值是String中的字符数。
fn:replace(string, before, after)
返回一个String对象。用参数after字符串替换参数string中所有出现参数before字符串的地方,并返回替换后的结果
fn:split(string, separator)
返回一个数组,以参数separator 为分割符分割参数string,分割后的每一部分就是数组的一个元素
fn:startsWith(string, prefix)
如果参数string以参数prefix开头,返回true
fn:substring(string, begin, end)
返回参数string部分字符串, 从参数begin开始到参数end位置,包括end位置的字符
fn:substringAfter(string, substring)
返回参数substring在参数string中后面的那一部分字符串
fn:substringBefore(string, substring)
返回参数substring在参数string中前面的那一部分字符串
fn:toLowerCase(string)
将参数string所有的字符变为小写,并将其返回
fn:toUpperCase(string)
将参数string所有的字符变为大写,并将其返回
fn:trim(string)
去除参数string 首尾的空格,并将其返回
用户设置语言环境
<fmt:setLocale value="expression" scope="scope" variant="expression"/>
设置时区
<fmt:setTimeZone value="expression" var="name" scope="scope"/>
用来格式化和显示日期和时间(数据输出)
<fmt:formatDate value="expression" timeZone="expression" type="time/date/both" dateStyle="default/short/medium/long/full"
timeStyle="style" var="name" pattern=" expression" scope="scope"/>
用来解析日期和时间值(数据输入)
<fmt:parseDate value="expression" type="field" dateStyle="style" timeStyle="style"
pattern="expression" var="name" timeZone=" expression" parseLocale="expression" scope="scope"/>
<fmt:parseDate type="field" dateStyle="style" timeStyle="style" pattern="expression" timeZone="expression"
var="name" parseLocale="expression" scope="scope">body content </fmt:parseDate>
<fmt:formatNumber value="expression" type="number/currency/percentage" pattern="expression"
currencyCode="expression" currencySymbol="expression" //货币的单位符号
maxIntegerDigits="expression" minIntegerDigits="expression"
maxFractionDigits="expression" minFractionDigits="expression" //小数点前后的位数
groupingUsed="true/false" var="name" scope="scope"/>
<fmt:parseNumber value="expression" type="type" pattern="expression"
parseLocale="expression" integerOnly="expression" var="name" scope="scope"/>
<fmt:parseNumber type="type" var="name" scope="scope" pattern="expression"
parseLocale="expression" integerOnly="expression"/>body content </fmt:parseNumber>
数据库
<sql:setDataSource dataSource="expression" var="name" scope="scope"/>
<sql:setDataSource url="expression" driver="expression" user="expression" password="expression"var="name" scope="scope"/>
查询
<sql:query var="" dataSource="">
sql语句
<sql:param value="${6}"/>
</sql:query>
<sql:query sql="expression" dataSource="expression" var="name" scope="scope" maxRows="expression" startRow="expression"/>
<sql:query sql="expression" dataSource="expression" var="name" scope="scope" maxRows="expression" startRow="expression">
<sql:param value="expression"/>
...
</sql:query>
<sql:query dataSource="expression" var="name" scope="scope" maxRows="expression" startRow="expression">
SQL statement
<sql:param value="expression"/>
...
</sql:query>
日期参数
<sql:dateParam value="expression" type="date/time/timestamp"/>
更新
<sql:update sql="expression" dataSource="expression" var="name" scope="scope"/>
<sql:update sql="expression" dataSource="expression" var="name" scope="scope">
<sql:param value="expression"/>
...
</sql:update>
<sql:update dataSource="expression" var="name" scope="scope">
SQL statement
<sql:param value="expression"/>
...
</sql:update>
事务
<sql:transaction dataSource=" expression" isolation="read_committed/read_uncommitted/repeatable_read/serializable">
<sql:query .../> or <sql:update .../>
</sql:transaction>