【问题标题】:Calling method with variable arguments from JSF 2.0使用 JSF 2.0 的可变参数调用方法
【发布时间】:2012-04-12 07:45:20
【问题描述】:

在一个支持 bean 中,我声明了以下方法

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

我正在尝试从 JSF 2.0 中调用它,如下所示:

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

它会抛出

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

如果我传递两个参数,那么它会抛出

Method hasPermission not found

我可以从 JSF 2.0 以某种方式调用可变参数方法吗?

【问题讨论】:

    标签: jsf variadic-functions


    【解决方案1】:

    可变参数在 EL 中不受官方支持。至少,EL specification. 中没有指定它,似乎也没有任何计划在即将到来的EL 3.0 中引入它。

    您需要寻找不同的解决方案。由于功能需求不明确,我不能推荐任何一个。


    更新 Tomcat 中提供的 Apache EL 解析器似乎支持这一点。 Glassfish 中提供的 Sun/Oracle EL 解析器至少不支持它。

    【讨论】:

      【解决方案2】:

      在 Tomcat 7 JSF 2.1.4 以下工作

      <?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>
      

      豆子:

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

      输出:点击 1,2,3

      [a, b, 1, 10, 10.0, 可变参数, [Ljava.lang.String;@4fa9cba5]

      [a, b, 1]

      [10, 10.0, 可变参数, [Ljava.lang.String;@26b923ee]

      这就是你要找的……因为你试图打电话的方式是空白的。

      【讨论】:

      • 有趣。那必须是Tomcat特定的。它在 Glassfish 中失败了。至少在 EL 规范中没有指定必须支持可变参数。
      • @BalusC 不是 glassfish 的活跃用户,但很高兴知道它在 glassfish 中的支持。
      • 嗨,我如何在 outputText 中使用它 --- &lt;h:outputText value="#{testBean.findTotalLengthStr('1123','123','421')}" /&gt; --- testBean public int findTotalLength(String ... strArray) { int totalLength = 0; for (int i = 0; i &lt; strArray.length; i++) { totalLength = totalLength + strArray[i].length(); } return totalLength; }
      猜你喜欢
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      相关资源
      最近更新 更多