项目使用 Spring Webflow 和 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>
后端代码(Spring注入(inject)的bean):
@Service("articleGroupManager")
public class ArticleGroupManagerImpl implements ArticleGroupManager{
public void setSelectedRow(ActionEvent event) {
String selectedIndex = (String)event.getComponent().getAttributes().get("selectedIndex");
if (selectedIndex == null) {
return;
}
}
}
属性“selectedIndex”始终为空。有人知道这里发生了什么吗?谢谢。
最佳答案
变量名称“rowIndex”表明您已在迭代组件内声明它,例如 <p:dataTable>
.
这确实行不通。组件树中实际上只有一个 JSF 组件,它在生成 HTML 输出期间被多次重复使用。 <f:attribute>
在创建组件时计算(只发生一次,在迭代之前很久!),而不是在组件基于当前迭代行生成 HTML 时计算。它确实总是 null
.
无论如何,有几种方法可以实现您的具体功能需求。最理智的方法是将它作为方法参数传递:
<p:commandButton value="Edit"
action="edit_article_group"
actionListener="#{articleGroupManager.setSelectedRow(rowIndex)}"
ajax="false" disabled="#{editGroupMode=='edit'}" />
与
public void setSelectedRow(Integer rowIndex) {
// ...
}
另见:
与具体问题无关,在这种特殊情况下,我只使用带有请求参数的 GET 链接来使请求幂等(可添加书签、可重新执行而不会影响服务器端,搜索机器人可抓取等)。另见 Communication in JSF 2.0 - Processing GET request parameters .
关于jsf - Primefaces 命令按钮 : f:attribute does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12496819/