Concatenate strings in JSF/JSP EL and Javascript [duplicate]
Asked Answered
P

2

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.

Pongee answered 3/2, 2010 at 14:51 Comment(1)
I'm using facelets. I edited the post to add that info, which indeed is relevantPongee
N
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);
    }
    
Nepheline answered 3/2, 2010 at 15:7 Comment(3)
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)Pongee
hm, no. I copied it from my configs, so it doesn't seem to do any harm either.Nepheline
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.Pongee
I
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();
Icefall answered 9/9, 2011 at 23:13 Comment(2)
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.Gutshall
In that case he should consider adding EL 2.2 support, especially for the ability to pass parameters in function calls.Pontificate

© 2022 - 2024 — McMap. All rights reserved.