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

标签 jsf jsf-2 spring-webflow

项目使用 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/

相关文章:

javascript - 部分刷新问题触发两次

jsf - java.lang.NullPointerException 在 java.net.URLEncoder.encode 在 com.sun.faces.context.UrlBuilder.addValuesToParameter

java - Spring Webflow 最佳实践

grails - Grails Web流:是否可以在URL中包含状态名称?

java - 在 Spring Webflow 中映射对象时出现 FlowInputMappingException

java - 带 selectitem 的 richface 树(复选框)

java - 检查 xhtml JSF 中的 session 访问

jsf-2 - 向导模式在 JSF 2.0 中使用什么范围?

java - 调试 JSF + PrimeFaces 应用程序

jsf - 空 h :dataTable in JSF renders empty row with invalid number of columns?