Call method with variable arguments from JSF 2.0

In the bean backup, I have declared the following method

public boolean hasPermission(Object... objects) {
...
}

      

And I'm trying to call it from JSF 2.0 like this:

<c:set var="hasPermission" scope="view" value="#{restrictions.hasPermission(entity)}" />

      

And he throws

javax.el.ELException:  Cannot convert Entity of class com.testing.Entity to class [Ljava.lang.Object;

      

If I pass two arguments, then it throws

Method hasPermission not found

      

Is it possible to somehow call varargs methods from JSF 2.0?

+3


source to share


2 answers


Varargs is not officially supported in EL. At least it's not specified anywhere in the EL spec. There are also no plans to introduce it in the upcoming EL 3.0 .

You need to look for another solution. Since the functional requirement is unclear, I can't suggest anyone.




Update it seems that the Apache EL parser provided in Tomcat supports this. This is at least not supported by the Sun / Oracle EL parser provided by Glassfish.

+2


source


In Tomcat 7 JSF 2.1.4 the following works

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h:form>
    <h:commandButton value="click 1"
        action="#{test.var('a','b',1,test.i,test.d,test.s,test.ss)}"/>
    <h:commandButton value="click 2"
        action="#{test.var('a','b',1)}"/>
    <h:commandButton value="click 3"
        action="#{test.var(test.i,test.d,test.s,test.ss)}"/>
</h:form>
</body>
</html>

      

Bean:

@ManagedBean
public class Test {

    private Integer i = 10;
    private Double d = 10.0;
    private String s = "varargs";
    private String[] ss = new String[]{"1","2","3"};
    public Integer getI() {
        return i;
    }
    public void setI(Integer i) {
        this.i = i;
    }
    public Double getD() {
        return d;
    }
    public void setD(Double d) {
        this.d = d;
    }
    public String getS() {
        return s;
    }
    public void setS(String s) {
        this.s = s;
    }
    public String[] getSs() {
        return ss;
    }
    public void setSs(String[] ss) {
        this.ss = ss;
    }

    public void var(Object...objects){
        System.out.println(Arrays.toString(objects));
    }
}

      



Conclusion: on click 1,2,3

[a, b, 1, 10, 10.0, varargs, [Ljava.lang.String; @ 4fa9cba5]

[a, b, 1]

[10, 10.0, varargs, [Ljava.lang.String; @ 26b923ee]

This is what you are looking for .... since the way you are trying to call is empty.

+2


source







All Articles