Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use static methods in EL

I'm facing a strange problem here with EL.

I just wanted to use String.join() in EL but it is not working.

#{String.join(',', myList)}

This is not doing anything in JSF except prevent my page to load. I know i can do this with <ui:repeat> but i need to use it in EL expression.

Any ideas ?

like image 917
AZVR Avatar asked Jan 25 '17 10:01

AZVR


People also ask

When static methods are used?

Static methods can be accessed without having to create a new object. A static method can only use and call other static methods or static data members. It is usually used to operate on input arguments (which can always accept), perform calculation and return value.

How do you call a static method in syntax?

A static method can be called directly from the class, without having to create an instance of the class. A static method can only access static variables; it cannot access instance variables. Since the static method refers to the class, the syntax to call or refer to a static method is: class name. method name.

What is static method with example?

A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance).

What is static method in Objective C?

From Wikipedia: Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance. This describes exactly what Objective-C's class methods are not.


2 Answers

You can't call a static method with EL. Create a Bean with a method to call String.join()

@RequestScoped
@Named
public class StringBean {

    public String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) {
        return String.join(delimiter, elements);
    }
}

So you can call #{stringBean.join(',', myList)}

like image 160
jklee Avatar answered Oct 26 '22 09:10

jklee


i found an approach to this.

register your util class as a bean in the faces-config.xml

<managed-bean>
    <managed-bean-name>String</managed-bean-name>
    <managed-bean-class>java.lang.String</managed-bean-class>
    <managed-bean-scope>application</managed-bean-scope>
</managed-bean>

I am using org.apache.commons.lang3.ArrayUtils and it is working for me.

like image 2
adrz1 Avatar answered Oct 26 '22 07:10

adrz1