EL表达式的应用
1.项目中应用el表达式的准备:
必须应用在servlet2.4规范基础以上 :在项目的web.xml配置文件中查看是否符合该要求。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
注:红色部分字体显示的版本需要2.4以上
-
在jsp中添加伪指令:将伪指令集写入到一个taglib.jsp中,往后所建立的jsp页面直接引入taglib.jsp即可。
常用taglib.jsp
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
注:红色部分字体为struts标签的引入与el无关
其他jsp页面引入taglib.jsp
<%@ include file="/WEB-INF/page/share/taglib.jsp" %>
2.常用el表达式
<c:out>基本语法
Without a body
<c:out value=”value” [escapeXml=”{true|false}”]
[default=”defaultValue”] />
With a body
<c:out value=”value” [escapeXml=”{true|false}”]>
default value
</c:out>
<c:set>基本语法
<c:set value=”value”
var=”varName” [scope=”{page|request|session|application}”]/>
<c:if>基本语法
<c:if test=”testCondition”
[var=”varName”] [scope=”{page|request|session|application}”]>
body content
</c:if>
<c:choose>基本语法
<c:choose>
body content (<when> and <otherwise> subtags)
</c:choose>
<c:when>基本语法
<c:when test=”testCondition”>
body content
</c:when>
<c:otherwise>基本语法
<c:otherwise>
conditional block
</c:otherwise>
<c:foreach>基本语法
<c:forEach [var=”varName”] items=”collection”
[varStatus=”varStatusName”]
[begin=”begin”] [end=”end”] [step=”step”]>
body content
</c:forEach>
组合应用
<c:forEach var="product" items="${products}" varStatus="status">
<c:choose>
<c:when test="${status.count % 2 == 0}">
even item
</c:when>
<c:otherwise>
odd item
</c:otherwise>
</c:choose>
</c:forEach>
<fmt:formatDate>基本语法
<fmt:formatDate value=”${now}” pattern="dd.MM.yy"/>
fn函数高级应用(转)
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 首尾的空格,并将其返回