Raistlin
Raistlin

Reputation: 427

Primefaces commandButton: f:attribute does not work

Project uses Spring Webflow and JSF (PrimeFaces). I have a p:commandButton with f:attribute

<p:commandButton disabled="#{editGroupMode=='edit'}" action="edit_article_group"  actionListener="#{articleGroupManager.setSelectedRow}" ajax="false" value="Edit">    
    <f:attribute name="selectedIndex" value="${rowIndex}" /> 
</p:commandButton>

Backend code (Spring injected 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;
    }
  }
}

The attribute "selectedIndex" is always null. Anybody knows what happened here? Thank you.

Upvotes: 3

Views: 10080

Answers (1)

BalusC
BalusC

Reputation: 1109542

The variable name "rowIndex" suggests that you've declared this inside an iterating component, such as <p:dataTable>.

This is then indeed not going to work. There's physically only one JSF component in the component tree which is reused multiple times during generating HTML output. The <f:attribute> is evaluated at the moment when the component is created (which happens only once, long before iteration!), not when the component generates HTML based on the currently iterated row. It would indeed always be null.

There are several ways to achieve your concrete functional requirement anyway. The most sane approach would be to just pass it as method argument:

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

with

public void setSelectedRow(Integer rowIndex) {
    // ...
}

See also:


Unrelated to the concrete problem, I'd in this particular case have used just a GET link with a request parameter to make the request idempotent (bookmarkable, re-executable without impact in server side, searchbot-crawlable, etc). See also Communication in JSF 2.0 - Processing GET request parameters.

Upvotes: 2

Related Questions