在 JSF/JSP EL 和 Javascript 中连接字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2192759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:35:58  来源:igfitidea点击:

Concatenate strings in JSF/JSP EL and Javascript

javajspjsfrichfacesel

提问by pakore

I'm having troubles with EL and javascript functions (JSF 1.2, Facelets, Richfaces 3.3.0GA). I have a page that includes another composition:

我在使用 EL 和 javascript 函数(JSF 1.2、Facelets、Richfaces 3.3.0GA)时遇到了麻烦。我有一个包含另一个组合的页面:

<ui:include src="/pages/panels/examinationPanel.xhtml">
<ui:param name="prefix" value="new" />

And in my ui:compositionI want to append the prefixto every id. For example:

在我中,ui:composition我想将 附加prefix到每个 id。例如:

<rich:modalPanel id="#{prefix}_examinationPanel">

That works ok.

这工作正常。

But the problem comes when I want to access the components in functions suchs as oncompleteI cannot get it to concatenate the strings properly. For example

但是当我想访问函数中的组件时问题就出现了,因为oncomplete我无法正确连接字符串。例如

oncomplete="#{rich:component('#{prefix}_examinationPanel')}.show();"

I've tried with fn:joinas well but it does not execute the function because it complains about errors when it finds "#" character. For example:

我也尝试过,fn:join但它不执行该函数,因为它在找到“#”字符时会抱怨错误。例如:

 oncomplete="#{rich:component(fn:join(#{prefix},'examinationPanel'))}.show()"

throws

投掷

SEVERE: Servlet.service() for servlet Faces Servlet threw exception org.apache.el.parser.ParseException: Encountered "fn:join( #" at line 1, column 33.

Encountered "fn:join( #"

Different errors if I brace it with brackets or with # and brackets.

如果我用括号或 # 和括号括起来,则会出现不同的错误。

What am I doing wrong?

我究竟做错了什么?

And another question, in a conditional command like

另一个问题,在条件命令中,例如

oncomplete="#{a}?#{b}:#{c}"

How can I "group" to be able to execute more actions when true or false? Por example something like this:

如何在 true 或 false 时“分组”以便能够执行更多操作?例如像这样的事情:

oncomplete="#{a}?(#{b}#{f}):(#{c}#{d}#{e})"

I've tried with parenthesis but does not parse it properly.

我试过括号,但没有正确解析它。

Thanks in advance.

提前致谢。

采纳答案by Bozho

Assuming you are using Facelets, here's a relatively good solution:

假设您使用的是 Facelets,这里有一个比较好的解决方案:

  • create functions.taglib.xmlin your WEB-INF
  • add a context param indicating the location:

    <context-param>
        <param-name>facelets.LIBRARIES</param-name>
        <param-value>
            /WEB-INF/functions.taglib.xml
        </param-value>
    </context-param>
    
  • In the xml put the following:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE facelet-taglib PUBLIC
      "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
      "https://facelets.dev.java.net/source/browse/*checkout*/facelets/src/etc/facelet-taglib_1_0.dtd">
    <facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
        <namespace>http://yournamespace.com/fnc</namespace>
        <function>
            <function-name>concat</function-name>
            <function-class>com.yourpackage.utils.Functions</function-class>
            <function-signature>
                java.lang.String concat(java.lang.String, java.lang.String)
            </function-signature>
        </function>
    </facelet-taglib>
    
  • in the page use the following:

    xmlns:fnc="http://yournamespace.com/fnc"
    ....
    oncomplete="#{rich:component(fnc:concat(prefix, '_examinationPanel'))}.show();"
    
  • finally, in the Functionclass define the simple method:

    public static String concat(String string1, String string2) {
       return string1.concat(string2);
    }
    
  • functions.taglib.xml在您的 WEB-INF 中创建
  • 添加指示位置的上下文参数:

    <context-param>
        <param-name>facelets.LIBRARIES</param-name>
        <param-value>
            /WEB-INF/functions.taglib.xml
        </param-value>
    </context-param>
    
  • 在 xml 中输入以下内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE facelet-taglib PUBLIC
      "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
      "https://facelets.dev.java.net/source/browse/*checkout*/facelets/src/etc/facelet-taglib_1_0.dtd">
    <facelet-taglib xmlns="http://java.sun.com/JSF/Facelet">
        <namespace>http://yournamespace.com/fnc</namespace>
        <function>
            <function-name>concat</function-name>
            <function-class>com.yourpackage.utils.Functions</function-class>
            <function-signature>
                java.lang.String concat(java.lang.String, java.lang.String)
            </function-signature>
        </function>
    </facelet-taglib>
    
  • 在页面中使用以下内容:

    xmlns:fnc="http://yournamespace.com/fnc"
    ....
    oncomplete="#{rich:component(fnc:concat(prefix, '_examinationPanel'))}.show();"
    
  • 最后,在Function类中定义简单方法:

    public static String concat(String string1, String string2) {
       return string1.concat(string2);
    }
    

回答by juanprq

a simpler solution is to manage the String in the EL like an Object and use the method concat from the class String, something like this:

一个更简单的解决方案是像管理对象一样管理 EL 中的字符串,并使用类 String 中的方法 concat,如下所示:

#{rich:component('constantString'.concat(variable))}.show();