【问题标题】:JSF CommandButton action not invoked [duplicate]未调用 JSF CommandButton 操作 [重复]
【发布时间】:2012-10-30 20:31:08
【问题描述】:

我遇到了未调用 JSF CommandButton 操作的问题。 我有一个托管 bean roleController,如

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

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

在我的 .jsf 文件中,我试图在 h:commandButton 上编辑调用 updateSelectedRole 操作,但它似乎不起作用。我尝试将方法名称更改为不正确的名称,并且没有引发异常 - 但是当我对其他表单执行相同操作时,会引发异常 - 所以很可能甚至没有调用该操作。

<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>

我发现它可能是嵌套表单造成的,但在这个例子中不是这样。这个问题的根源可能是我的导航规则吗?

<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>

【问题讨论】:

    标签: java jsf jakarta-ee


    【解决方案1】:
    <h:commandButton value="#{msg.buttonUpdate}" 
      action="#{roleController.updateSelectedRole()}"/>
    

    改成

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

    您的操作调用不需要括号。

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

    并且方法action方法需要返回一个字符串

    public String updateSelectedRole() {
    
        System.out.println(selectedRole.getRole());
        return "true";
    }
    

    【讨论】:

    • 我已经尝试过了,没有帮助。
    • @DanielCisek 也看到了第二个问题。更新了答案。
    • 很遗憾,它不起作用。
    • 自 EL 2.2 起支持括号。如果 OP 使用的是 EL 2.1 或更早版本,他会得到一个ELException。所以这绝对不是问题。返回字符串不是必需的。 Void也很好。如果它真的无效,它也会抛出一个ELException。然而事实并非如此。
    【解决方案2】:

    未调用该操作,因为未呈现命令按钮组件。

    在处理表单提交期间,父面板组组件上的rendered="${param.action == 'edit'}" 被重新评估(以防止被篡改/黑客请求)。但是,由于您显然没有在回发中保留该请求参数(至少,代码中没有任何其他证明),因此它的计算结果为 false。因此不会渲染父面板组组件,包括它的所有子组件。

    您需要确保在表单提交期间命令按钮的rendered 属性及其所有父组件的计算结果为true。在这种特殊情况下,您可以通过在命令按钮本身中包含 &lt;f:param&gt; 来保留请求参数来实现这一点。

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

    另见:


    与具体问题无关,建议在 JSF 中禁止使用${},否则会导致不必要的混淆和问题。一直坚持#{}

    【讨论】:

      【解决方案3】:

      来自客户端的请求必须封装在一个 HTTP 表单中

      【讨论】:

        【解决方案4】:

        此订单更改在我的情况下有效:

        <h:form>
          <h:panelGroup rendered="${param.action == 'edit'}">
          .
          .
          .
          </h:panelGroup>
        </h:form>
        

        【讨论】:

        • 您确定没有更改任何其他内容?或者这个变化触发了部署纠正了其他一些错误?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-20
        • 2016-01-12
        • 1970-01-01
        • 1970-01-01
        • 2015-08-28
        • 2014-05-23
        相关资源
        最近更新 更多