JSP EL 自定义函数

1.编写java类
ArticleUtil.JAVA

package com.wangyi.template;

public class ArticleUtil {
public static String addd(String s, int length) throws Exception{
byte[] bytes = s.getBytes("Unicode");
int n = 0; // 表示当前的字节数
int i = 2; // 要截取的字节数,从第3个字节开始
for (; i < bytes.length && n < length; i++){
// 奇数位置,如3、5、7等,为UCS2编码中两个字节的第二个字节
if (i % 2 == 1){
n++; // 在UCS2第二个字节时n加1
}else{
// 当UCS2编码的第一个字节不等于0时,该UCS2字符为汉字,一个汉字算两个字节
if (bytes[i] != 0){
n++;
}
}
}
// 如果i为奇数时,处理成偶数
if (i % 2 == 1){
// 该UCS2字符是汉字时,去掉这个截一半的汉字
if (bytes[i - 1] != 0){
i = i - 1;
// 该UCS2字符是字母或数字,则保留该字符
}else{
i = i + 1;
}
}
return new String(bytes, 0, i, "Unicode");
}
}


2.编写 wangyifunc.tag放置到WEB-INF下的任何子目录


<taglib 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-jsptaglibrary_2_0.xsd"
version="2.0">

<description>wangyi template taglib</description>
<tlib-version>1.0</tlib-version>
<short-name>wy</short-name>
<uri>http://template.wangyi.com/article/taglib</uri>
<function>
<description>Reverses the characters in the given String</description>
<name>add</name>
<function-class>com.wangyi.template.ArticleUtil</function-class>
<function-signature>java.lang.String addd( java.lang.String, int )</function-signature>
</function>

</taglib>

3编写jsp测试页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="wy" uri="http://template.wangyi.com/article/taglib"%>

<%
request.setAttribute("aa","你好1你好2你好3你好4你好5你好6你好7你好8你好9你好0你好1你好2你好3你好4你好5你好6你好7你好8你好9你好0");
//request.setAttribute("ints",new Integer(5));
%>
${wy:add(aa,32)}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值