jstl与struts2标签截取字符串.docVIP

  • 2
  • 0
  • 约6.08千字
  • 约 4页
  • 2017-08-05 发布于河南
  • 举报
jstl和struts2标签截取字符串 jstl jstl以前在jsp页面截取字符串时都是用Java表达式,感觉在jsp页面用多了Java表达式不是很好(那时主要是不知道jstl标签怎么去截取字符串,所以就只能用Java表达式了)。今天在网上找了些相关资料,原来jstl标签是可以截取字符串的,主要有两种方法: 一、用jstl原有的标签库中方法(fn函数) ????? c:set var=testStr value=做一个截取字符串长度的测试 ????? c:choose ????????? c:when test=${fn:length(testStr) 10} ????????????? c:out value=${fn:substring(testStr, 0, 10)} / ????????? /c:when ???????? c:otherwise ??????????? c:out value=${testStr} / ????????? /c:otherwise ????? /c:choose 注:记得要引用标签库 ??????? %@ taglib uri=/jsp/jstl/core prefix=c% ??????? %@ taglib uri=/jsp/jstl/functions prefix=fn% 二、自定义函数 jstl自带的标签有时候可能满足不了我们的需求,这时我们就要用到自定义函数。 新建一个类,定义好所需要的方法: Java代码: package com.uisk.util;??? ?? /** * 定义EL自定义函数 * */?? public class UFunction {??? ?? ??? /** ????? * 获取一个字符串的长度 ????? * ????? * @param str ????? * @return int ????? */?? ??? public static int getLen(String str) {??? ??????? return str.length();??? ???? }??? ?? ??? /** ????? * 截取字符串 ????? * ????? * @param str ????? * @param start ????? * @param end ????? * @return String ????? */?? ??? public static String substr(String str, int start, int end) {??? ??????? return str.substring(start, end);??? ???? }??? ?? ??? /** ????? * 两数相除获取整数结果 ????? *?? ????? * @param first ????? * @param second ????? * @return int ????? */?? ??? public static int chufa(int first, int second) {??? ??????? return first / second;??? ???? }??? }? 再建一个tld文件放在WEB-INF目录下面对其进行配置: ?xml version=1.0 encoding=UTF-8???? taglib xmlns=/xml/ns/j2ee?? ???? xmlns:xsi=/2001/XMLSchema-instance?? ???? xsi:schemaLocation=/xml/ns/j2ee /xml/ns/j2ee/web-jsptaglibrary_2_0.xsd?? ???? version=2.0??? ???? tlib-version1.0/tlib-version??? ???? short-namelen/short-name??? ???? function??? ???????? descriptioncalculate string length/description!-- 对这个EL方法的描述?? --??? ???????? namegetLen/name!-- 调用EL方法的名称 --??? ???????? function-classmon.UFunction/function-class??? ???????? function-signature??? ??????????? int getLe

文档评论(0)

1亿VIP精品文档

相关文档