Use static methods in EL
Asked Answered
F

3

6

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 ?

Fortunate answered 25/1, 2017 at 10:25 Comment(0)
H
6

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)}

Hartnett answered 25/1, 2017 at 10:33 Comment(4)
I was trying to find a way to prevent this case... Thanks mate !Fortunate
There's another way though. You'll find out now you have the right terms/keywords.Informant
You mean EL 3.0 for call static methods?Hartnett
@Informant I wrote your answers about declaring EL with tag but i feel dumb i don't understand how can i'm supposed to do in my case (using String join) ....Fortunate
H
2

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.

Hippel answered 20/4, 2017 at 10:1 Comment(0)
P
0

You can write a custom function that exposes your static method as a function in EL. A similar question has been answered here.

Perorate answered 25/1, 2017 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.