【问题标题】:Execute some JSF business logic before POST to external URL在 POST 到外部 URL 之前执行一些 JSF 业务逻辑
【发布时间】:2012-12-27 14:01:19
【问题描述】:

我的 JSF 页面上有以下表格,该表格发布到外部信用卡处理器。 我想改用 JSF 操作方法,以便编写一些业务逻辑。如何通过操作方法发布帖子并包含以下字段?

        <form action="#{checkoutBean.sagepayURL}" method="post"
            id="SagePayForm" name="SagePayForm">

            <input type="hidden" name="VPSProtocol"
                value="#{checkoutBean.sagepayMessage.vpsProtocol}" /> <input
                type="hidden" name="TxType"
                value="#{checkoutBean.sagepayMessage.transactionType}" /> <input
                type="hidden" name="Vendor"
                value="#{checkoutBean.sagepayMessage.vendor}" /> <input
                type="hidden" name="Crypt" class="crypt"
                value="#{checkoutBean.sagepayMessage.crypt}" /> <input
                type="submit" 
                value="Proceed to Checkout" />

        </form>

【问题讨论】:

    标签: java jsf url post jsf-2


    【解决方案1】:

    您可以使用 JavaScript 提交一个隐藏的 JSF ajax 表单,该表单进而呈现一个提交付款表单的脚本。

    <form action="#{checkoutBean.sagepayURL}" method="post"
        id="SagePayForm" name="SagePayForm">
        ...
        <input type="button" value="Proceed to Checkout" 
            onclick="document.getElementById('hiddenForm:link').click()" />
    </form>
    
    <h:form id="hiddenForm" style="display:none">
        <h:commandLink id="link" action="#{checkoutBean.preprocess}">
            <f:ajax render="@form" />
        </h:commandLink>
        <h:outputScript rendered="#{checkoutBean.preprocessed}">
            document.getElementById("SagePayForm").submit();
        </h:outputScript>
    </h:form>
    

    private boolean preprocessed; // +getter
    
    public void preprocess() {
        // ...
    
        preprocessed = true;
    }
    

    根据您的问题历史,您似乎使用的是OmniFaces,在这种情况下,您还可以使用&lt;o:commandScript&gt; 对其进行简化,如下所示:

    <form action="#{checkoutBean.sagepayURL}" method="post"
        id="SagePayForm" name="SagePayForm">
        ...
        <input type="button" value="Proceed to Checkout" onclick="preprocess()" />
    </form>
    
    <h:form>
        <o:commandScript name="preprocess" action="#{checkoutBean.preprocess}"
            oncomplete="document.getElementById('SagePayForm').submit()" />
    </h:form>
    

    【讨论】:

    • 这不起作用...我收到以下错误:httpError:HTTP 传输返回 0 状态代码。这通常是混合 ajax 和完整请求的结果。
    • 如果您没有使用答案中所示的“死”&lt;input type="button"&gt;,请将return false; 添加到onclick 的末尾以阻止调用按钮的默认操作。
    猜你喜欢
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 2017-12-17
    • 2015-11-04
    相关资源
    最近更新 更多