1

我正在尝试使用带有 jsfbean 参数的自定义 EL 函数调用方法。

<c:set var="test1" value="${cx:methodName('para')}" scope="session"/>
        <h:outputText value="#{test1}"/>

我做了以下 id TLD 并将其放入 WEB-INF

  <?xml version="1.0" encoding="UTF-8" ?>
    <taglib 
        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"
        version="2.1">

        <tlib-version>1.0</tlib-version>
        <short-name>Custom_Functions</short-name>
        <uri>http://example.com/functions</uri>

        <function>
            <name>methodName</name>
            <function-class>funcionclass(Jsfbean)</function-class>
            <function-signature>java.lang.String methodName(java.lang.String)</function-signature>
        </function>
    </taglib>

我也在 web.xml 中配置

<jsp-config> 
        <taglib> 
               <taglib-uri>http://example.com/functions</taglib-uri> 
               <taglib-location>/WEB-INF/functions.tld</taglib-location> 
        </taglib> 
</jsp-config>

仍然出现 Function not found 错误。

以下是方法代码

public static String methodName(String s1) throws Exception
    {
        return "Kshitij";
    }

任何身体都可以提供帮助。

4

2 回答 2

3

您在使用 Facelets 时创建了一个 JSP EL 函数。这是行不通的。另外,您的<function-class>声明不正确。它应该指定全限定类名 (FQN)。例如com.example.FunctionClass

将 taglib 文件重命名为/WEB-INF/functions.taglib.xml以下更新内容:

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 
    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-facelettaglibrary_2_0.xsd"
    version="2.0">
    <namespace>http://example.com/functions</namespace>

    <function>
        <function-name>methodName</function-name>
        <function-class>com.example.FunctionClass</function-class>
        <function-signature>java.lang.String methodName(java.lang.String)</function-signature>
    </function>
</facelet-taglib>

然后你需要注册它web.xml按如下方式注册它(不要忘记删除旧的 JSP taglib 注册!):

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/functions.taglib.xml</param-value>
</context-param>

最后通过以下 XML 命名空间在您的视图中声明它

xmlns:cx="http://example.com/functions"

与具体问题无关,有一种替代方法,可能更容易:安装 JBoss EL。这样,您将能够像在新的 EL 2.2 中一样调用 bean 上的方法。

于 2012-04-19T12:21:08.700 回答
1

您仍然需要在您的 jsp 中包含参考:

<%@taglib prefix="cx" uri="http://example.com/functions"%>
于 2012-04-19T11:37:26.170 回答