如何在jsp中的scriptlet方法中传递正则表达式值?

时间:2014-12-03 16:13:59

标签: jsp jstl jsp-tags

考虑案例

double Method(double variable) 
{ 
 ..............
 ..............  
 return someDoubleValue; 
}

<c:set var="t_variable" value="123.999"/>

<fmt:formatNumber value="Method(${t_variable})" />

这里我不知道如何在Method中传递表达式语言变量。谁能说出正确的方法来传递这个?

1 个答案:

答案 0 :(得分:0)

方法是为JTSL创建自定义函数:

在/ WEB-INF

下创建.tld文件
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
    <tlib-version>1.0</tlib-version>
    <short-name>tlds</short-name>
    <uri>http://www.sometld.com/tlds</uri>
    <function>
        <name>someMethod</name>
        <function-class>org.domain.Functions</function-class>
        <function-signature>double someMethod(double)</function-signature>
    </function>
</taglib>

使用静态方法创建一个类

package org.domain;

public class Functions
{
    public static double someMethod(double value)
    {
        ...
        return someValue;
    }
}

并使用它

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://www.sometld.com/tlds" prefix="mfn" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
            ${mfn:someMethod(123.999)}
    </body>
</html>