Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF CommandButton action not invoked [duplicate]

I'm having a problem with JSF CommandButton action not being invoked. I have a managed bean roleController, as in

@ManagedBean(name = "roleController")
@RequestScoped
public class RoleController {
        public Role getSelectedRole() {
    return selectedRole;
}

public void updateSelectedRole() {
    System.out.println(selectedRole.getRole());
}

In my .jsf file I'm trying to edit invoke updateSelectedRole action on h:commandButton, but it doesn't seem to work. I've tried to change the method name to incorrect one, and there's no exception thrown - but when I do the same with other form, the exception is thrown - so most likely the action isn't even invoked.

<h:panelGroup rendered="${param.action == 'edit'}">
    <h:form>
        <ol>
            <li>
                <label for="ID">
                    <h:outputText value="#{msg.roleEditID}" />
                </label>
                <h:inputText readonly="true" 
                    value="#{roleController.selectedRole.id}" />
            </li>
            <li>
                <label for="Role">
                    <h:outputText value="#{msg.roleEditRole}" />
                </label>
                <h:inputText value="#{roleController.selectedRole.role}" />
            </li>
            <li>
                <h:commandButton value="#{msg.buttonUpdate}" 
                    action="#{roleController.updateSelectedRole()}"/>
            </li>
        </ol>
    </h:form>
</h:panelGroup>

I found that it may be caused be nested forms, but that's not the case in this example. Is it possible that the root of this problem is my navigation rule?

<navigation-rule>
    <from-view-id>/admin/roles.xhtml</from-view-id>
    <navigation-case>
        <to-view-id>/admin/roles.xhtml</to-view-id>
        <from-outcome>true</from-outcome>
        <redirect>
            <view-param>
                <name>action</name>
                <value>edit</value>
            </view-param>
        </redirect>
    </navigation-case>
</navigation-rule>
like image 460
Daniel Cisek Avatar asked Nov 10 '12 21:11

Daniel Cisek


1 Answers

The action is not been invoked, because the command button component is not rendered.

During processing of the form submit, the rendered="${param.action == 'edit'}" on the parent panel group component is been re-evaluated (as safeguard against tampered/hacked requests). However, as you're apparently not retaining that request parameter in the postback (at least, nothing in the code proves otherwise), it evaluates to false. And thus the parent panel group component is not rendered, including all of its childen.

You need to make sure that the rendered attribute of the command button and all of its parent components evaluates to true during the form submit. In this particular case you can achieve that by retaining the request parameter by including a <f:param> in the command button itself.

<h:commandButton ...>
    <f:param name="action" value="#{param.action}" />
</h:commandButton>

See also:

  • commandButton/commandLink/ajax action/listener method not invoked or input value not updated - Point 5 applies to you

Unrelated to the concrete problem, it's recommend to ban the usage of ${} in JSF as it would otherwise lead to unnecessary confusion and questions. Just stick to #{} all the time.

like image 117
BalusC Avatar answered Nov 18 '22 06:11

BalusC