#{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" ...
"+=" ...
"=" ...
我尝试了使用String作为参数和Boolean对象的相同代码,并且效果很好,但是使用自定义Object也会产生相同的错误,并且如果我们将复杂的对象(例如UIComponent)传递给了同样的错误。
我正在使用JSF 2.2,欢迎任何帮助。
答案 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>
#{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});