【问题标题】:Display dialog window if some criteria is not met in JSF form如果在 JSF 表单中不满足某些条件,则显示对话框窗口
【发布时间】:2020-05-03 20:47:03
【问题描述】:

我正在尝试使用 AJAX 实现 JSF 按钮,如果不满足某些条件,则使用 AJAX 形成该按钮。我试过这个例子:

<h:form id="paymentform" enctype="multipart/form-data" x:data-form-output="form-output-global" x:data-form-type="contact" class="rd-mailform text-left">

<h:commandButton id="buy" class="btn btn-primary" type="submit" value="Put" action="#{dashboard.calculateProcessing}"  >
    <f:ajax render="@form" execute="@form" x:data-toggle="modal" x:data-target="#demoModal"/>  // if #{dashboard.amount} <= 0 call modal dialog 
</h:commandButton>

<div class="modal fade" id="demoModal" role="dialog">
.....
</div>

</h:form>



@Named
@SessionScoped
public class Dashboard implements Serializable {

    private static final long serialVersionUID = -2363083605115312742L;

    private double amount;

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }
}

如果我这样编辑按钮:

<h:commandButton id="buy" class="btn btn-primary" type="submit" value="Put" action="#{dashboard.calculateProcessing}" x:data-toggle="modal" x:data-target="#demoModal" >

显示对话框并提交表单。

但是如果不满足 Bean 的某些条件,我找不到如何不提交表单的解决方案。您能指导我如何找到解决方案吗?

【问题讨论】:

  • 我更新了答案。这是我能想到的。

标签: jsf jsf-2


【解决方案1】:

您想根据 bean 中的条件执行f:ajax。基本上,f:ajax 应该在条件为真时呈现,否则不呈现。 每个 JSF 组件都有一个 rendered 值。

您的代码应如下所示:

<h:commandButton id="buy" class="btn btn-primary" type="submit" value="Put" action="#{dashboard.calculateProcessing}"  >
    <f:ajax render="@form" execute="@form" x:data-toggle="modal" x:data-target="#demoModal" rendered="#{dashboard.amount <= 0}"/>
</h:commandButton>

这也可以通过多个f:ajax 来完成,如下所示:

<h:commandButton>
    <f:ajax render="@form" rendered="#{bean.condition}" />
    <f:ajax execute="@form" rendered="#{bean.int < 0}" />
    <f:ajax action="#{bean.action}" rendered="#{bean.property == null}" />
</h:commandButton>

基本上,如果 rendered 属性为 false,则不会导致客户端接收到 HTML。 More about rendered

您也可以使用jstl tags 来定义组件是否被渲染。 像这样使用:

<h:commandButton>
    <c:if test="#{bean.condition}>
        <!-- f:ajax here-->
    </c:if>

    <c:choose>
        <c:when test="#{bean.condition}">
            <!-- f:ajax here-->
        </c:when>

        <c:otherwise>
            <!-- f:ajax here-->
        </c:otherwise>
    </c:choose>
</h:commandButton>

如要在commandButton 中调用动态操作,请参阅this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多