Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 690
Raistlin Avatar asked Apr 29 '26 15:04

Raistlin


1 Answers

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:

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

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.

like image 120
BalusC Avatar answered May 01 '26 09:05

BalusC