Jsf Primefaces commandButton:f:属性不起作用

Jsf Primefaces commandButton:f:属性不起作用,jsf,jsf-2,spring-webflow,Jsf,Jsf 2,Spring Webflow,项目使用SpringWebFlow和JSF(PrimeFaces)。我有一个带有f:attribute的p:commandButton <p:commandButton disabled="#{editGroupMode=='edit'}" action="edit_article_group" actionListener="#{articleGroupManager.setSelectedRow}" ajax="false" value="Edit"> <

项目使用SpringWebFlow和JSF(PrimeFaces)。我有一个带有f:attribute的p:commandButton

<p:commandButton disabled="#{editGroupMode=='edit'}" action="edit_article_group"  actionListener="#{articleGroupManager.setSelectedRow}" ajax="false" value="Edit">    
    <f:attribute name="selectedIndex" value="${rowIndex}" /> 
</p:commandButton>
属性“selectedIndex”始终为空。有人知道这里发生了什么吗?谢谢。

变量名“rowIndex”表明您已经在迭代组件(如
)中声明了这一点

这是行不通的。在组件树中,实际上只有一个JSF组件,在生成HTML输出时可以多次重用。
是在组件创建时(只发生一次,在迭代之前很久!)计算的,而不是在组件基于当前迭代行生成HTML时计算的。它实际上总是
null

无论如何,有几种方法可以实现您的具体功能需求。最明智的方法是将其作为方法参数传递:

<p:commandButton value="Edit" 
    action="edit_article_group"  
    actionListener="#{articleGroupManager.setSelectedRow(rowIndex)}" 
    ajax="false" disabled="#{editGroupMode=='edit'}" />  
另见:


与具体问题无关,在这种情况下,我只会使用带有请求参数的GET链接,使请求幂等(可书签、可在服务器端重新执行而不受影响、可搜索机器人爬网等)。另请参见。

如何将
value=“#{rowIndex}”
替换为
$
也替换为
setSelectedRow
将该方法重命名为类似
updateSelectedRowValue
(不要在方法名称中使用set/get前缀)它不应该是
操作
,而不是
操作侦听器
?方法签名呢?即使在阅读了你关于action vs actionListener的所有答案后,我想我还是不能完全理解它。@RinaldoPJr:这确实是一个极端的例子。此特定示例基本上取代了
(如果不支持EL 2.2,则可以使用该示例)。请注意,此处不调用任何业务操作,只设置了一个setter并执行了导航。但是该方法是否必须遵循签名(接收
ActionEvent
作为参数,而不接收其他内容)?@Rinaldo:只有在EL中未指定自定义参数时,才需要
ActionEvent
。这比JSF更具体。如果在EL中显式指定参数,它将获得优先权。这确实令人困惑,因为在JSF规范中没有提到EL中的这种可能的覆盖。我认为这是一篇好文章的主题。:)
<p:commandButton value="Edit" 
    action="edit_article_group"  
    actionListener="#{articleGroupManager.setSelectedRow(rowIndex)}" 
    ajax="false" disabled="#{editGroupMode=='edit'}" />  
public void setSelectedRow(Integer rowIndex) {
    // ...
}