20

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>
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
Daniel Cisek
  • 931
  • 3
  • 9
  • 23

4 Answers4

30

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:


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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0
<h:commandButton value="#{msg.buttonUpdate}" 
  action="#{roleController.updateSelectedRole()}"/>

change it to

 <h:commandButton value="#{msg.buttonUpdate}" 
  action="#{roleController.updateSelectedRole}"/>

You don't need the brackets for your action call.

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

and the method action method needs to return a string

public String updateSelectedRole() {

    System.out.println(selectedRole.getRole());
    return "true";
}
Udo Held
  • 12,314
  • 11
  • 67
  • 93
  • I've already tried that, didn't help. – Daniel Cisek Nov 10 '12 at 21:53
  • @DanielCisek saw a second problem as well. Updated the answer. – Udo Held Nov 10 '12 at 21:55
  • Unfortunately, it does not work. – Daniel Cisek Nov 10 '12 at 22:52
  • 8
    Brackets are supported since EL 2.2. If OP had used EL 2.1 or older, he would have gotten an `ELException` on that. So that definitely isn't the problem. Returning string is not a requirement. Void is also perfectly fine. If it was really invalid, it would have thrown an `ELException` as well. This is however not the case. – BalusC Nov 11 '12 at 00:31
0

the request from the client must be encapsulated within an HTTP form

-2

this order change worked in my case:

<h:form>
  <h:panelGroup rendered="${param.action == 'edit'}">
  .
  .
  .
  </h:panelGroup>
</h:form>
  • Sure you did not change anything else? Or that this change triggered a deployment correcting some other error? – Kukeltje May 30 '18 at 11:51