创建包含参数类型为 Object 的方法的 MethodExpression 会生成解析异常

Create MethodExpression containing a method with parameter of type Object generates parse exception

#{bean.method(TheObjectInstance)}
public class TheObject
{
   public String value0 = "value0";
}
    TheObject object = new TheObject();

    FacesContext context = FacesContext.getCurrentInstance();
    Application application = context.getApplication();
    ExpressionFactory factory = application.getExpressionFactory();

    //Create method expression
    MethodExpression methodExpression = factory.createMethodExpression(
       context.getELContext(), 
       "#{bean.method(" + object + ")}", 
       null, 
       new Class<?>[] {TheObject.class});
javax.servlet.ServletException: Encountered "@" at line 1, column 87.
Was expecting one of:
    "." ...
    "(" ...
    ")" ...
    "[" ...
    "," ...
    ";" ...
    ">" ...
    "gt" ...
    "<" ...
    "lt" ...
    ">=" ...
    "ge" ...
    "<=" ...
    "le" ...
    "==" ...
    "eq" ...
    "!=" ...
    "ne" ...
    "&&" ...
    "and" ...
    "||" ...
    "or" ...
    "*" ...
    "+" ...
    "-" ...
    "?" ...
    "/" ...
    "div" ...
    "%" ...
    "mod" ...
    "+=" ...
    "=" ...
  • 要使用包含对象作为参数的 bean 方法创建 MethodExpression #{bean.method(object)},我们应该使用 HTML 页 var=object 中声明的 var 的名称。
    <h:form>
     <h:datatable var="object" value="#{bean.objects}">
      <h:commandbutton value="test" actionlistenner="#{bean.method(object)}"/>
    </h:datatable>
    </h:form>
  • 如果我们想要生成相同的 MethodExpression #{bean.method(object)},我们将不得不生成完整的 html 元素,包括一个父 html 元素,在我们的例子中是一个包含对object var=object 然后在 MethodExpression
  • 的代码中
    //Wrong implementation: the object is converted as object.getClass().toString()
    MethodExpression methodExpression = factory.createMethodExpression(
       context.getELContext(), 
       "#{bean.method(" + object + ")}", 
       null, 
       new Class<?>[] {TheObject.class});

    //Right implementation: we refer to object referenced by the datatable var. 
    MethodExpression methodExpression = factory.createMethodExpression(
       context.getELContext(), 
       "#{bean.method(object)}", 
       null, 
       new Class<?>[] {TheObject.class});