jsf - Primefaces 命令按钮 : f:attribute does not work

项目使用 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) {
    // ...
}

另见:

  • JSTL in JSF2 Facelets... makes sense?
  • How can I pass selected row to commandLink inside dataTable?

与具体问题无关,在这种特殊情况下,我只使用带有请求参数的 GET 链接来使请求幂等(可添加书签、可重新执行而不会影响服务器端,搜索机器人可抓取等)。另见 Communication in JSF 2.0 - Processing GET request parameters .

https://stackoverflow.com/questions/12496819/

相关文章:

winforms - 如何指定 ListView 的 SelectionMode 属性?

azure - 我的 cspack 命令应该是什么来打包 azure 项目

regex - 在 Dreamweaver 中自动执行多个正则表达式搜索/替换查询

php - 使用 TCPDF 的多行文本?

visual-studio-2010 - 没有 Team Foundation Server 的 V

fonts - python matplotlib mathtext AMSeuler 字体

python - 子列表具有共同元素的列表的合并列表

build - IntelliJ编译多模块项目

ruby-on-rails-3 - heroku rails logging 不显示输出?

php - 为嵌套对象扩展 json_encode