Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 963
pakore Avatar asked Feb 03 '10 14:02

pakore


People also ask

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

Can you use a double in string concatenation?

The “+” operator with a String acts as a concatenation operator. Whenever you add a String value to a double using the “+” operator, both values are concatenated resulting a String object. In-fact adding a double value to String is the easiest way to convert a double value to Strings.

How do you concatenate strings in JavaScript?

The + Operator The same + operator you use for adding two numbers can be used to concatenate two strings. You can also use += , where a += b is a shorthand for a = a + b . If the left hand side of the + operator is a string, JavaScript will coerce the right hand side to a string.


2 Answers

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);
    }
    
like image 145
Bozho Avatar answered Oct 01 '22 19:10

Bozho


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();
like image 28
juanprq Avatar answered Oct 01 '22 19:10

juanprq