JSF/PrimeFaces:编程<;f:setPropertyActionListener>;on<;p:commandButton>;不开火
我们要求允许用户在所有数据表中配置列的顺序,包括上面有操作按钮的列,这里是JSF/PrimeFaces:编程<;f:setPropertyActionListener>;on<;p:commandButton>;不开火,jsf,jsf-2,primefaces,datatable,commandbutton,Jsf,Jsf 2,Primefaces,Datatable,Commandbutton,我们要求允许用户在所有数据表中配置列的顺序,包括上面有操作按钮的列,这里是p:commandButtons 因此,我们对所有列使用绑定,必须手动实例化。对于只显示一些字符串、布尔值、日期和数字的所有列,所有这些都可以正常工作,但是在将p:commandButtons添加到包含一个或多个s的列时会出现问题 ELContext elContext = FacesContext.getCurrentInstance().getELContext(); ExpressionFactory
p:commandButton
s
因此,我们对所有列使用绑定,必须手动实例化。对于只显示一些字符串、布尔值、日期和数字的所有列,所有这些都可以正常工作,但是在将p:commandButton
s添加到包含一个或多个
s的列时会出现问题
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
CommandButton button = new CommandButton();
button.setIcon( "ui-icon ui-icon-pencil" );
button.setProcess( "@this" );
button.setUpdate( ":compliance-case-dialog :compliance-case-form:data" );
button.setOncomplete( "complianceCaseDialog.show();" );
ValueExpression targetExpression = factory.createValueExpression( elContext, "#{complianceCaseManager.selectedComplianceCaseWithRefresh}", ComplianceCase.class );
ValueExpression valueExpression = factory.createValueExpression( elContext, "#{coca}", ComplianceCase.class );
button.addActionListener( new SetPropertyActionListenerImpl( targetExpression, valueExpression ) );
column.getChildren().add( button ); // add to Column instance
此按钮“正确”显示数据表的当前实体(由var=“coca”
定义)的ComplianceCaseManager
类“setSelectedComplianceCaseWithRefresh(ComplianceCase selectedComplianceCase)
方法,然后显示对话框
<p:dataTable id="data"
widgetVar="resultTable"
value="#{complianceCaseManager.complianceCases}"
var="coca"
...>
</p:dataTable>
线路
<f:setPropertyActionListener target="#{complianceCaseManager.selectedComplianceCaseWithRefresh}" value="#{coca}" />
创建AFAIK是为了在目标上调用setter,在值上调用setter,因此我假设ValueExpression
s就足够了
更新2: 试图通过EL 2.2方法调用设置当前实体也不起作用 代码:
String方法ExpressionString=“#”+“{complianceCaseManager.selectedComplianceCaseWithRefresh(coca)}”;
MethodExpression MethodExpression=factory.createMethodExpression(elContext,methodExpressionString,null,新类[]{ComplianceCase.Class});
addActionListener(新的MethodExpressionActionListener(methodExpression));
什么都不叫
更新3: 以下是正确的
代码:
// traditional <f:setPropertyActionListener target="#{complianceCaseManager.selectedComplianceCaseWithRefresh}" value="#{coca}" />
ValueExpression targetExpression = factory.createValueExpression( elContext, "#{complianceCaseManager.selectedComplianceCaseWithRefresh}", ComplianceCase.class );
ValueExpression valueExpression = factory.createValueExpression( elContext, "#{coca}", ComplianceCase.class );
button.addActionListener( new SetPropertyActionListenerImpl( targetExpression, valueExpression ) );
//传统的
ValueExpression targetExpression=factory.createValueExpression(elContext,“{complianceCaseManager.selectedComplianceCaseWithRefresh}”,ComplianceCase.class);
ValueExpression ValueExpression=factory.createValueExpression(elContext,“{coca}”,ComplianceCase.class);
addActionListener(新的SetPropertyActionListenerImpl(targetExpression,valueExpression));
非常感谢科洛索斯。当实例化PrimeFaces
CommandButton
时,请记住调用setId()
,actionListener应创建为MethodExpression
而不是ValueExpression
:
。使用
button.addActionListener( new MethodExpressionActionListener(targetExpression));
id
属性。为了安全起见,添加
button.setId("theButton");
编辑:在动态创建命令组件时,必须设置id
属性我曾经使用lambda表达式来解决这个问题:
private final ActionListener actionListener = (ActionEvent event) -> {
//code to execute
};
然后就是:
comandButton.setId(id)代码>
commandButon.addActionListener(ActionListener)代码>
它不能工作。根据和Java文档,
SetPropertyActionListenerImpl
构造函数的签名是com.sun.faces.taglib.jsf_core.SetPropertyActionListenerImpl.SetPropertyActionListenerImpl(ValueExpression目标,ValueExpression值)
MethodExpression
不继承自ValueExpression
,仅继承Expression
。这可能是使用直接EL 2.2方法调用的最后手段。然而,我想知道如何将“老派”JSF1.2
s翻译成编程变体。查看我的更新以查看普通JSF中的
。您是否设置了id
属性?OMG.:-哦,这真的解决了!请将其作为答案发布。:-)它已经在那里了:)谢谢你的帮助。
MethodExpression targetExpression = factory.createMethodExpression( elContext, "#{complianceCaseManager.selectedComplianceCaseWithRefresh(coca)}",null,new Class[]{ComplianceCase.class} );
button.addActionListener( new MethodExpressionActionListener(targetExpression));
button.setId("theButton");
private final ActionListener actionListener = (ActionEvent event) -> {
//code to execute
};