我的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>
您可以使用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,在这种情况下,您还可以使用<o:commandScript>
简化它,如下所示:
<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>