JavaWeb课程系列

4.1 核心标签库:URL操作

重定向

<%-- 使用context属性指定项目虚拟路径,使用url属性指定重定向的目标位置 --%>

<c:redirect

 context="${pageContext.request.contextPath }"

 url="/target2.jsp" />

传递请求参数

<%-- 使用context属性指定项目虚拟路径,使用url属性指定重定向的目标位置 --%>

<c:redirect

 context="${pageContext.request.contextPath }"

 url="/target2.jsp">

<%-- 使用name属性指定请求参数名,使用value属性指定请求参数值 --%>

<c:param name="sayHi" value="hi..."></c:param>

</c:redirect>

5.函数标签库(fn

  • 简介

在JSTL中专门定义了一些函数给EL表达式使用,来完成一些简单通用的功能,大部分都一些字符串相关操作。

2)格式

${fn:函数名(函数参数) }

3)功能

执行对应的函数并显示

  • 使用

① 在JSP中导入JSTL中的EL函数标签库

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

② 使用el函数

       ${fn:length(person1.name) }

       ${fn:substring(s1,1,3) }

5)标签库提供的功能

函数名

举例

功能

contains

<c:if test="${fn:contains(name, searchString)}”>

是否包含

containsIgnoreCase

<c:if test="${fn: containsIgnoreCase (name,

searchString)}”>

是否包含,不区分大小写

endsWith

<c:if test="${fn:endsWith(filename, ".txt")}">

是否以.txt结束

startsWith

<c:if test="${fn:startsWith(product.id, "100-")}">

是否以100- 开始

indexOf

${fn:indexOf(name, "-")}

返回-的索引

join

${fn:join(array, ";")}

将数组元素使用;拼接

length

${fn:length(shoppingCart.products)}

返回集合中元素个数

replace

${fn:replace(text, "a", "?")}

使用?替换所有a

split

${fn:split(customerNames, ";")}

使用;将字符串分割

substring

${fn:substring(zip, index, end)}

字符串截取,[index,end)

End=-1,表示截取剩下所有

substringAfter

${fn:substringAfter(zip, "-")}

截取-之后的所有元素,不包括-

substringBefore

${fn:substringBefore(zip, "-")}

截取-之前的所有元素,不包括-

toLowerCase

${fn.toLowerCase(product.name)}

全部转换为小写

UpperCase

${fn.UpperCase(product.name)}

全部转化为大写

trim

${fn.trim(name)}

去除字符串两头的空串