创建包含参数类型为 Object 的方法的 MethodExpression 会生成解析异常
Create MethodExpression containing a method with parameter of type Object generates parse exception
- 我正在通过 java 代码直接构建 MethodExpression。
- 它将表示调用带有参数的 bean 方法,如 follow 。
#{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" ...
"+=" ...
"=" ...
我用一个字符串作为参数和一个布尔对象尝试了相同的代码,它工作正常,但使用自定义对象会产生同样的错误,如果我们传递一个复杂的对象,例如 a UIComponent.
我正在使用 JSF 2.2,欢迎任何帮助。
- 要使用包含对象作为参数的 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});
- 我正在通过 java 代码直接构建 MethodExpression。
- 它将表示调用带有参数的 bean 方法,如 follow 。
#{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" ...
"+=" ...
"=" ...
我用一个字符串作为参数和一个布尔对象尝试了相同的代码,它工作正常,但使用自定义对象会产生同样的错误,如果我们传递一个复杂的对象,例如 a UIComponent.
我正在使用 JSF 2.2,欢迎任何帮助。
- 要使用包含对象作为参数的 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 元素,在我们的例子中是一个包含对objectvar=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});