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

时间:2019-05-14 08:09:13

标签: jsf uicomponents methodexpression

  • 我正在直接通过Java代码构建MethodExpression。
  • 它将代表对参数为follow的bean方法的调用。
#{bean.method(TheObjectInstance)}
  • 该对象是一个简单的自定义pojo对象
public class TheObject
{
   public String value0 = "value0";
}
  • 我们现在创建如下的MethodExpression。
    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" ...
    "+=" ...
    "=" ...
  • 我尝试了使用String作为参数和Boolean对象的相同代码,并且效果很好,但是使用自定义Object也会产生相同的错误,并且如果我们将复杂的对象(例如UIComponent)传递给了同样的错误。

  • p>
  • 我正在使用JSF 2.2,欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

  • 要使用包含对象作为参数#{bean.method(object)}的bean方法创建MethodExpression,我们应该使用在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元素(在本例中为包含引用对象var=object的数据表),然后在代码中方法表达式
    //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});