您的位置:首页 > Web前端 > JavaScript

jsp自定义标签-----EL表达式中连接两个字符串

2016-01-29 17:19 597 查看
我们发现在jsp页面输出两个字符串变量 不能用+号连接,否则编译器会把它当成数值类型然后报错。

那怎么在jsp页面连接两个字符串呢。

因为 java代码中可以用concat把字符串连接起来。

那么我们可以在jsp中调用 java的函数方法来  把两个字符串连接起来。

jsp自定义标签就是应用在这种场景的方法之一: 在jsp页面调用java中的方法对数据进行处理。

jsp自定义标签 的使用流程如下(以连接两个字符串为例):

首先我们需要在项目中新建一个tld文件来定义标签

建立标签库

新建如图



cf.tld的内容如下:

<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>custom JSTL 1.1 functions library</description>
<display-name>JSTL functions</display-name>
<tlib-version>1.1</tlib-version>
<short-name>cf</short-name>
<uri>com.test.web.view.function</uri>

<function>
<description>判断对象是否为空字符串</description>
<name>isEmpty</name>
<function-class>com.test.util.StringUtils</function-class>
<function-signature>java.lang.Boolean isEmpty(java.lang.Object)</function-signature>
</function>

<function>
<description>连接两个字符串</description>
<name>contact</name>
<function-class>com.test.util.ObjectUtils</function-class>
<function-signature>java.lang.String contact(java.lang.String,java.lang.String)</function-
signature>
/function>

<function>
<description>把数字转换成千分位显示</description>
<name>formatNumToThousand</name>
<function-class>com.test.util.ObjectUtils</function-class>
<function-signature>java.lang.String formatNumToThousand(java.lang.Double)</function-signature>
</function>

function>
<description>获取数字的整数部分</description>
<name>getInt</name>
<function-class>com.test.util.ObjectUtils</function-class>
<function-signature>java.lang.Integer getInt(java.lang.Double)</function-signature>
</function>

<function>
<description>判断数字是否有小数位</description>
<name>getHaveFloatNumFlag</name>
<function-class>com.test.util.ObjectUtils</function-class>
<function-signature>java.lang.Boolean getHaveFloatNumFlag(java.lang.Double)</function-

signature>
</function>

</taglib>



新建类定义方法

在上图定义的路径(com.test.util)下新建ObjectUtils类内容如下:

package com.test.util;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Deprecated
public final class ObjectUtils {

/**
* 字符串是否为空
*
* @param string
* @return
*/
private static boolean isEmpty(String string) {
int strLen;
if (string == null || (strLen = string.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (Character.isWhitespace(string.charAt(i)) == false) {
return false;
}
}
return true;
}

/**
* 用于在EL中连接字符串header+end
*
* @param header
* @param end
* @return
*/
public static String contact(String header, String end) {
return header.concat(end);
}

/**
* 用于在EL中获取数字整数部分
*
* @param header
* @param end
* @return
*/
public static int  getInt(Double num) {
return num.intValue();
}

/**
* 用于在EL中判断数字是否有小数位
*
* @param header
* @param end
* @return
*/
public static Boolean  getHaveFloatNumFlag(Double num) {

return num>num.intValue();
}

/**
* 用于在EL中把数字格式化成千分逗号显示
*
* @param header
* @param end
* @return
*/
public static String  formatNumToThousand(Double num) {
String strTemp=String.valueOf(num.intValue());
BigDecimal bg=new BigDecimal(strTemp);
String str=bg.toPlainString();
boolean neg = false;
if (str.startsWith("-")){  //处理负数
str = str.substring(1);
neg = true;
}
String tail = null;
if (str.indexOf('.') != -1){ //处理小数点
tail = str.substring(str.indexOf('.'));
str = str.substring(0, str.indexOf('.'));
}
StringBuilder sb = new StringBuilder(str);
sb.reverse();
for (int i = 3; i < sb.length(); i += 4){
sb.insert(i, ',');
}
sb.reverse();
if (neg){
sb.insert(0, '-');
}
if (tail != null){
sb.append(tail);
}
return sb.toString();
}

}


   /**

     * 用于在EL中连接字符串header+end

     * 

     * @param header

     * @param end

     * @return

     */

    public static String contact(String header, String end) {

        return header.concat(end);

    }

就是 我们需要的方法,注意 必须使用static,否则 jsp页面找不到该方法。

而且返回类型和参数类型要与标签库中的 定义一致。

使用标签

然后我们就可以在jsp页面中使用标签了。

首先要引入标签库:

<%@ taglib uri="com.test.web.view.function" prefix="cf"%>
这里的uri  和 标签名都要与我们在标签库中的定义一致,详见上面的图。



在jsp中使用如下:

cf:contact('detail?id=',afterArticle.id)



成功把detail?id=和 后端的值afterArticle.id 连接在一起。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: