JSTL 使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需要在页面调用对象的某些方法,例如我需要调用字符串的length方法来获取字符串的 长度时,在以往的开发过程中我们必须把对象先转为String类,然后在调用其length方法,这样的代码繁琐而且容易出错。

因此JSTL内置了几个用于字符串操作的方法,可以直接在表达式中使用,大大的简化了代码,提供代码的可读性。在JSTL的表达是中要使用一个函数,其格式如下

     

 ${ns:methodName(args....)}

在使用这些函数之前必须在JSP中引入标准函数的声明

     

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

下面是JSTL中自带的方法列表以及其描述

函数名 函数说明 使用举例
fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}">
fn:containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}">
fn:endsWith 判断字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}">
fn:escapeXml 把一些字符转成XML表示,例如<字符应该转为&lt; ${fn:escapeXml(param:info)}
fn:indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")}
fn:join 将数组中的数据联合成一个新字符串,并使用指定字符格开 ${fn:join(array, ";")}
fn:length 获取字符串的长度,或者数组的大小 ${fn:length(shoppingCart.products)}
fn:replace 替换字符串中指定的字符 ${fn:replace(text, "-", "•")}
fn:split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")}
fn:startsWith 判断字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}">
fn:substring 获取子串 ${fn:substring(zip, 6, -1)}
fn:substringAfter

获取从某个字符所在位置开始的子串

${fn:substringAfter(zip, "-")}
fn:substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")}
fn:toLowerCase 转为小写 ${fn.toLowerCase(product.name)}
fn:toUpperCase 转为大写字符 ${fn.UpperCase(product.name)}
fn:trim 去除字符串前后的空格 ${fn.trim(name)}

函数

描述

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 首尾的空格,并将其返回

http://blog.csdn.net/qingwangyoucao/article/details/6218069

EL表达式中fn函数的使用及截取字符串

文章分类:Java编程 关键字: el表达式中fn函数的使用及截取字符串

做了一个月的项目,下面把我经常用到的,在今天写了几篇文章,方便以后的查阅.
fn函数:

  1. 使用前在Jsp页面的首部加上以下代码:
  2. <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  3. 下面是JSTL中自带的方法列表以及其描述
  4. 函数名 函数说明 使用举例
  5. fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}">
  6. fn:containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}">
  7. fn:endsWith 判断字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}">
  8. fn:escapeXml 把一些字符转成XML表示,例如 <字符应该转为&lt; ${fn:escapeXml(param:info)}
  9. fn:indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")}
  10. fn:join 将数组中的数据联合成一个新字符串,并使用指定字符格开 ${fn:join(array, ";")}
  11. fn:length 获取字符串的长度,或者数组的大小 ${fn:length(shoppingCart.products)}
  12. fn:replace 替换字符串中指定的字符 ${fn:replace(text, "-", "•")}
  13. fn:split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")}
  14. fn:startsWith 判断字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}">
  15. fn:substring 获取子串 ${fn:substring(zip, 6, -1)}
  16. fn:substringAfter 获取从某个字符所在位置开始的子串
  17. ${fn:substringAfter(zip, "-")}
  18. fn:substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")}
  19. fn:toLowerCase 转为小写 ${fn.toLowerCase(product.name)}
  20. fn:toUpperCase 转为大写字符 ${fn.UpperCase(product.name)}
  21. fn:trim 去除字符串前后的空格 ${fn.trim(name)}
  1. 使用前在Jsp页面的首部加上以下代码:
  2. <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
  3. 下面是JSTL中自带的方法列表以及其描述
  4. 函数名 函数说明 使用举例
  5. fn:contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}">
  6. fn:containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}">
  7. fn:endsWith 判断字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}">
  8. fn:escapeXml 把一些字符转成XML表示,例如 <字符应该转为&lt; ${fn:escapeXml(param:info)}
  9. fn:indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")}
  10. fn:join 将数组中的数据联合成一个新字符串,并使用指定字符格开 ${fn:join(array, ";")}
  11. fn:length 获取字符串的长度,或者数组的大小 ${fn:length(shoppingCart.products)}
  12. fn:replace 替换字符串中指定的字符 ${fn:replace(text, "-", "•")}
  13. fn:split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")}
  14. fn:startsWith 判断字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}">
  15. fn:substring 获取子串 ${fn:substring(zip, 6, -1)}
  16. fn:substringAfter 获取从某个字符所在位置开始的子串
  17. ${fn:substringAfter(zip, "-")}
  18. fn:substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")}
  19. fn:toLowerCase 转为小写 ${fn.toLowerCase(product.name)}
  20. fn:toUpperCase 转为大写字符 ${fn.UpperCase(product.name)}
  21. fn:trim 去除字符串前后的空格 ${fn.trim(name)}

函数 描述

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 首尾的空格,并将其返回

EL表达式中fn函数 (转载)的更多相关文章

  1. EL表达式中fn函数

    JSTL 使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需 ...

  2. El表达式和fn函数一起使用

     如下: ${a%1>0?a:(fn:substringBefore(a, "."))} 

  3. EL表达式中如何截取字符串

    EL表达式中如何截取字符串 可以截取,用fn函数:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/ ...

  4. java、el表达式中保留小数的方法

    Java中: import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; p ...

  5. EL表达式中引用隐式变量

    除了在jsp中9大隐式变量(在前面文章也叫预定义变量)在转化成为servlet后_jspService中可以看到: public void _jspService(final javax.servle ...

  6. 关于EL表达式中requestScope和param区别

    今天演示EL表达式的时候发现自己jsp的基础实在是薄弱,在这个很简单的问题上迷惑了很久. 首先在看遇到的问题: 在浏览器地址输入,表示传入一个参数test,值为123 http://localhost ...

  7. 在jsp中怎么使用Cookie?el表达式中获取cookie的问题

    初学jsp,不清楚cookie的使用方法,希望高手指点一下!   一般来说有两种办法,在JSP中使用Java的嵌入脚本. 例如: 写入Cookie <html> <head>. ...

  8. EL表达式中的11个隐式对象

    EL表达式中定义了11个隐式对象,使用这些隐式对象可以很方便地读取到Cookie.HTTP请求消息头字段.请求参数.Web应用程序中的初始化参数的信息,EL表达式中的隐式对象具体如下: 隐式对象 作用 ...

  9. JS中如何使用EL表达式中的对象

    JS中如何使用EL表达式中的对象 2017年09月25日 15:33:09 lhpnba 阅读数:4859   1.js中使用el表达式要加双引号或单引号:'${list}' 2.js变量获取el表达 ...

随机推荐

  1. height:100%和height:auto的区别

    一直不明白height:100%和height:auto的区别,最近在制作前端页面时都用了height:100%;overflow:hidden; ,可是有些浏览器出现莫名的奇妙的问题,但换成heig ...

  2. R-数据结构

    目录 数据类型(模式) 字符型 数值型 逻辑型 整形 复数型(虚数) 原生型(字节) 数据结构 向量 矩阵 数组 数据框 列表 数据类型 数据结构  向量 用于存储数值型.字符型或逻辑型数据的一维数组 ...

  3. 如何在发布博客时插入复杂公式——Open Live Writer

    1.http://latex.codecogs.com/eqneditor/editor.php 2.使用Word发布

  4. hdu1757 A Simple Math Problem

    Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x > ...

  5. Linux中source是什么指令?

    命令用法: source FileName 作用:在当前bash环境下读取并执行FileName中的命令. 注:该命令通常用命令“.”来替代. 如:source /etc/profile 与 .  / ...

  6. 第12天 android studio

    1. http://jingyan.baidu.com/article/215817f7888dc21eda14230d.html Gradle DSL method not found:‘andro ...

  7. hibernate之persistence错误

    错误码: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityM ...

  8. jQuery - 动态创建iframe并加载页面

    <html> <head> <script language="JavaScript" src="jquery-1.11.1.min.js& ...

  9. PHP Closure类Bind与BindTo方法

    Closure类为闭包类,PHP中闭包都是Closure的实例: 1 $func = function(){}; 2 var_dump($func instanceof Closure); 输出 bo ...

  10. Android本地数据存储之SQLite关系型数据库 ——SQLiteDatabase

    数据库的创建,获取,执行sql语句: 框架搭建:dao 思考: 1.数据库保存在哪里? 2.如何创建数据库?如何创建表? 3.如何更新数据库?如何更改表的列数据? 4.如何获取数据库? 5.如何修改数 ...