11

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.

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
pakore
  • 11,395
  • 12
  • 43
  • 62

2 Answers2

20

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);
    }
    
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks for the answer, I'm going to try it right now. One question, the trailing ";" for the param-value is intentional? And the other, isn't this the same as "fn:join" (which does not work) – pakore Feb 03 '10 at 15:17
  • hm, no. I copied it from my configs, so it doesn't seem to do any harm either. – Bozho Feb 03 '10 at 15:18
  • I've tried it and it works. And besides, I've learn a lesson about creating your own namespaces and functions :). Thanks for the fast and complete answer. – pakore Feb 03 '10 at 15:44
15

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:

#{rich:component('constantString'.concat(variable))}.show();
juanprq
  • 151
  • 1
  • 4
  • 4
    Note that this only works in EL 2.2 or JBoss EL (for EL 2.1). OP didn't indicate that he's using Servlet 3.0 nor JBoss Seam. – BalusC Sep 09 '11 at 23:18
  • In that case he should consider adding EL 2.2 support, especially for the ability to pass parameters in function calls. – Christophe Roussy Apr 17 '12 at 09:07