我们要求允许用户配置所有数据表中列的顺序,包括在其上具有操作按钮的列,此处为p:commandButton
。
因此,我们对所有必须手动实例化的列都使用了绑定。对于仅显示某些字符串,布尔值,日期和数字的所有列,都可以正常工作,但是,当将p:commandButton
添加到具有一个或多个<f:setPropertyActionListener>
的列时,就会出现问题。
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
此按钮“正确”显示一个
<p:dialog>
,但在显示该对话框之前,应使用数据表的当前实体(由ComplianceCaseManager
定义)调用setSelectedComplianceCaseWithRefresh( ComplianceCase selectedComplianceCase )
类的var="coca"
方法。<p:dataTable id="data"
widgetVar="resultTable"
value="#{complianceCaseManager.complianceCases}"
var="coca"
...>
</p:dataTable>
调试显示从未调用
setSelectedComplianceCaseWithRefresh( ComplianceCase selectedComplianceCase )
方法。问:
怎么了?您如何解决这个问题?
PS:配置为PrimeFaces 3.4.2,Mojarra 2.1.22,GlassFish 3.1.2.2,Java 7
更新1:
这是我想翻译为程序化的p:commandButton:
<p:commandButton icon="ui-icon ui-icon-pencil"
title="#{msg['complianceCaseManager.data.edit.button.hint']}"
process="@this"
update=":compliance-case-dialog :compliance-case-form:data"
oncomplete="complianceCaseDialog.show();">
<p:resetInput target=":unlocked-form" />
<f:setPropertyActionListener target="#{complianceCaseManager.selectedComplianceCaseWithRefresh}" value="#{coca}" />
<f:setPropertyActionListener target="#{complianceCaseManager.mode}" value="EDIT" />
</p:commandButton>
线
<f:setPropertyActionListener target="#{complianceCaseManager.selectedComplianceCaseWithRefresh}" value="#{coca}" />
是我必须去上班的那个。
kolossus创建
MethodExpressionActionListener
的解决方案不适用于我的代码: String targetExpressionString = "#{complianceCaseManager.selectedComplianceCaseWithRefresh}";
String valueExpressionString = "#{coca}";
MethodExpression targetMethodExpression = factory.createMethodExpression( elContext, targetExpressionString, null, new Class<?>[]{ ComplianceCase.class } );
MethodExpression valueMethodExpression = factory.createMethodExpression( elContext, valueExpressionString, ComplianceCase.class, new Class<?>[0] );
button.addActionListener( new MethodExpressionActionListener( targetMethodExpression, valueMethodExpression ) );
进行AFAIK的目的是在目标上调用设置器,在值上调用设置器,因此我假设
ValueExpression
就足够了。更新2:
尝试通过EL 2.2方法调用设置当前实体也不起作用。
码:
String methodExpressionString = "#" + "{complianceCaseManager.selectedComplianceCaseWithRefresh(coca)}";
MethodExpression methodExpression = factory.createMethodExpression( elContext, methodExpressionString, null, new Class<?>[]{ ComplianceCase.class } );
button.addActionListener( new MethodExpressionActionListener( methodExpression ) );
什么都没叫。
更新3:
这是正确的
<:setPropertyActionListener>
代码: // 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 ) );
非常感谢kolossus。实例化PrimeFaces
setId()
时,请记住要调用CommandButton
。 最佳答案
应该将actionListener创建为MethodExpression
而不是ValueExpression
:
我假设factory instanceOf ExpressionFactory
。使用createMethodExpression factory
MethodExpression targetExpression = factory.createMethodExpression( elContext, "#{complianceCaseManager.selectedComplianceCaseWithRefresh(coca)}",null,new Class[]{ComplianceCase.class} );
将侦听器添加到组件:
button.addActionListener( new MethodExpressionActionListener(targetExpression));
与当前问题不完全相关,您还省略了组件的
id
属性。为了安全起见,请添加button.setId("theButton");
编辑:动态创建命令组件时,您绝对必须设置
id
属性关于jsf - JSF/PrimeFaces:<p:commandButton>上的编程<f:setPropertyActionListener>不触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17343336/