Concatenate strings in JSF/JSP EL and Javascript

pakore picture pakore · Feb 3, 2010 · Viewed 28k times · Source

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:

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

And in my ui:composition I want to append the prefix to every id. For example:

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

That works ok.

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

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

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

 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:

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

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

Thanks in advance.

Answer

Bozho picture Bozho · Feb 3, 2010

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

  • create functions.taglib.xml in 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 Function class define the simple method:

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