0

I have problems with invoking commandLink action in dataTable.

<h:body>
    <h:dataTable value="#{manageStaffAccountBean.accounts}" var="staff">
        <h:column>
            #{staff.surname} 
        </h:column>
        <h:column>
            <h:form>                        
                <h:commandButton value="Change status" 
                    action="#{manageStaffAccountBean.changeActivity(staff)}" />
            </h:form>
        </h:column>
    </h:dataTable>
</h:body>

By click on "Change status" i need to invoke changeActivity() method in bean

Managed Bean code:

@Named
@Scope("request")
public class ManageStaffAccountBean implements Serializable {   

    private List<Staff> accounts = null;

    public String changeActivity(Staff staff){          
        System.out.println(staff.getId());

        return "manageStaffAccounts";
    }

    public void updateAccountsList(){
        accounts = staffService.findAll();      
    }

    // ...
}

However, it is not invoked. Can you help me to find the problem?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
saresh
  • 28
  • 4
  • 2
    So, the method `changeActivity(Staff)` is in first place already not invoked? How exactly is the DAO/service code then relevant to the problem? You can just replace them all by a simple `System.out.println()` in `changeActivity(Staff)` method for pure demonstration to reduce code noise and make the question better focused. That template composition is also unhelpful in excluding probable causes. Please post code in single-file flavor. You should provide the most minimal snippet which one would copy'n'paste straight into a templateless ``. See also http://stackoverflow.com/tags/jsf/info – BalusC Aug 05 '15 at 19:10
  • Isn't `@Scope("request")` a Spring annotation? Usually you don't attempt to mix Spring with JSF and CDI: it's an unconventional configuration. – DavidS Aug 05 '15 at 21:12
  • 1
    @DavidS Spring supports the JSR330, it resolves `@Inject` and `@Named`. – Guillermo Aug 05 '15 at 21:21
  • @BalusC, I suppose the method of the bean is invoked because the page is refreshing. OP, if you want your page not refresh make your bean method return type as void. I don't think it's a jsf problem and I'm unfamiliar with the annotations you use. You might want to double check those. – Ced Aug 06 '15 at 02:09
  • @Ced the method may not be invoked: The form is submitted but the `commandButton.action` is processed and bean's method is invoked in *invoke application* phase of JSF lifecycle. In the previous phases, the `datatable` is restored what could be affecting the recognition of the commandButton action and consequently never call the bean's method. – Guillermo Aug 06 '15 at 04:52
  • @Guillermo I don't understand the second part of your comment. When the database is restored it doesn't reload the data, it takes the already loaded data ? – Ced Aug 06 '15 at 13:03
  • Shouldn't he annotate updateAccountsList() with PostConstruct? – Ced Aug 06 '15 at 13:08

1 Answers1

0

I found the solution. If we use h:commandButton inside h:dataTable, our manage bean should have scope "session", or button wouldn't work. Just JSF magic

saresh
  • 28
  • 4
  • No. Use view scope. Or just fix initialization logic. See point 4 in duplicate. And in the future questions, please try to ask more focused questions instead of overly broad questions. I already edited your question to show off of you could have done it. – BalusC Aug 06 '15 at 15:59