jsp中通过自定义fn标签实现指定长度字符,多余的用……表示

[/code]    今天做到在jsp 页面中显示数据的时候,由于数据比较多,一下子表格就拉开了 ,变形了。想到以前在ASP.NET中做过,但那完全不一样,那里直接可能调用后台代码,而在jsp页面中呢?我也试了一下,但是又不能次页面中的数据传进去也是不行。<%= Util.cutString("${user.name}",10)%>,没想到这样也没用,只是一个字符串死的。

解决方法 ,我想到了 jstl 标签中的 fn 标签,但找了一下,都用不上,没有自己想用的方法。我想自己可不可以加个方法进去呢?就找 jstl 的源码。和打成的JAR 文件,看了一下 有关fn 标签的实现,没想到只有一个Functions 类型里面有一些静态的方法修改一下配置文件就Ok了。这是加了以后Functions 类的代码:
[code="java"]
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
*
* Portions Copyright Apache Software Foundation.
*/

package org.apache.taglibs.standard.functions;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.jsp.JspTagException;

import org.apache.taglibs.standard.resources.Resources;
import org.apache.taglibs.standard.tag.common.core.Util;

/**
* <p>JSTL Functions</p>
*
* @author Pierre Delisle
*/

public class Functions {

//*********************************************************************
// String capitalization

/**
* Converts all of the characters of the input string to upper case.
*/
public static String toUpperCase(String input) {
return input.toUpperCase();
}

/**
* Converts all of the characters of the input string to lower case.
*/
public static String toLowerCase(String input) {
return input.toLowerCase();
}

//*********************************************************************
// Substring processing

public static int indexOf(String input, String substring) {
if (input == null) input = "";
if (substring == null) substring = "";
return input.indexOf(substring);
}

public static boolean contains(String input, String substring) {
return indexOf(input, substring) != -1;
}

public static boolean containsIgnoreCase(String input, String substring) {
if (input == null) input = "";
if (substring == null) substring = "";
String inputUC = input.toUpperCase();
String substringUC = substring.toUpperCase();
return indexOf(inputUC, substringUC) != -1;
}

public static boolean startsWith(String input, String substring) {
if (input == null) input = "";
if (substring == null) substring = "";
return input.startsWith(substring);
}

public static boolean endsWith(String input, String substring) {
if (input == null) input = "";
if (substring == null) substring = "";
int index = input.indexOf(substring);
if (index == -1) return false;
if (index == 0 && substring.length() == 0) return true;
return (index == input.length() - substring.length());
}

public static String substring(String input, int beginIndex, int endIndex) {
if (input == null) input = "";
if (beginIndex >= input.length()) return "";
if (beginIndex < 0) beginIndex = 0;
if (endIndex < 0 || endIndex > input.length()) endIndex = input.length();
if (endIndex < beginIndex) return "";
return input.substring(beginIndex, endIndex);
}

public static String substringAfter(String input, String substring) {
if (input == null) input = "";
if (input.length() == 0) return "";
if (substring == null) substring = "";
if (substring.length() == 0) return input;

int index = input.indexOf(substring);
if (index == -1) {
return "";
} else {
return input.substring(index+substring.length());
}
}

public static String substringBefore(String input, String substring) {
if (input == null) input = "";
if (input.length() == 0) return "";
if (substring == null) substring = "";
if (substring.length() == 0) return "";

int index = input.indexOf(substring);
if (index == -1) {
return "";
} else {
return input.substring(0, index);
}
}

//*********************************************************************
// Character replacement

public static String escapeXml(String input) {
if (input == null) return "";
return Util.escapeXml(input);
}

public static String trim(String input) {
if (input == null) return "";
return input.trim();
}

public static String replace(
String input,
String substringBefore,
String substringAfter)
{
if (input == null) input = "";
if (input.length() == 0) return "";
if (substringBefore == null) substringBefore = "";
if (substringBefore.length() == 0) return input;

StringBuffer buf = new StringBuffer(input.length());
int startIndex = 0;
int index;
while ((index = input.indexOf(substringBefore, startIndex)) != -1) {
buf.append(input.substring(startIndex, index)).append(substringAfter);
startIndex = index + substringBefore.length();
}
return buf.append(input.substring(startIndex)).toString();
}

public static String[] split(
String input,
String delimiters)
{
String[] array;
if (input == null) input = "";
if (input.length() == 0) {
array = new String[1];
array[0] = "";
return array;
}

if (delimiters == null) delimiters = "";

StringTokenizer tok = new StringTokenizer(input, delimiters);
int count = tok.countTokens();
array = new String[count];
int i = 0;
while (tok.hasMoreTokens()) {
array[i++] = tok.nextToken();
}
return array;
}

//*********************************************************************
// Collections processing

public static int length(Object obj) throws JspTagException {
if (obj == null) return 0;

if (obj instanceof String) return ((String)obj).length();
if (obj instanceof Collection) return ((Collection)obj).size();
if (obj instanceof Map) return ((Map)obj).size();

int count = 0;
if (obj instanceof Iterator) {
Iterator iter = (Iterator)obj;
count = 0;
while (iter.hasNext()) {
count++;
iter.next();
}
return count;
}
if (obj instanceof Enumeration) {
Enumeration enum_ = (Enumeration)obj;
count = 0;
while (enum_.hasMoreElements()) {
count++;
enum_.nextElement();
}
return count;
}
try {
count = Array.getLength(obj);
return count;
} catch (IllegalArgumentException ex) {}
throw new JspTagException(Resources.getMessage("FOREACH_BAD_ITEMS"));
}

public static String join(String[] array, String separator) {
if (array == null) return "";
if (separator == null) separator = "";

StringBuffer buf = new StringBuffer();
for (int i=0; i<array.length; i++) {
buf.append(array[i]);
if (i < array.length-1) buf.append(separator);
}

return buf.toString();
}

public static String cutString(String input,int length){
if (input.length()<=length) {
return input;
}else{
return input.substring(0,length)+"...";
}
}

}



最后一段是自己加的方法。

和在fn.tld 中增加了


<function>
<description>
Returns a subset of a string.
</description>
<name>cutString</name>
<function-class>org.apache.taglibs.standard.functions.Functions</function-class>
<function-signature>java.lang.String cutString(java.lang.String, int)</function-signature>
<example>
P.O. Box: ${fn:cutstring(zip, 6)}
</example>
</function>


在把编译后的Functions.class 文件去替换 jstl1.2.jar解压后的相应位置的Functions.class 和 fn.tld 文件 再做成Jar包就可以用了(做成Jar包的方法,按解压出来的目录不变,替换以后,打个zip 包,修改后缀为jar 就可以了这种方法比较方便)

在页面中使用就是



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

${fn:cutString(product.enDesc ,15) }

这里也提供做好了的jstl1.2.jar文件,不用自己再去改了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值