How do I prevent form resubmission in Seam?

怎甘沉沦 提交于 2019-11-30 20:27:47

Seam comes with the s:token component and it is what you are looking for: http://docs.jboss.org/seam/2.2.0.GA/reference/en-US/html/controls.html#d0e28933

To avoid double submits caused by impatiently pressing the submit button you could use a piece of Javascript which disables the submit button a few ms after onclick.

Example:

<h:commandButton 
    id="foo"
    value="submit"
    action="#{bean.submit}"
    onclick="setTimeout('document.getElementById(\'' + this.id + '\').disabled=true;', 50);"
/>

To avoid double submits by pressing the back button and ignoring the browser's warning that you may risk to resend the data, you need to implement the Post-Redirect-Get (PRG) pattern.

In JSF this can be done in basically 2 ways. Either using <redirect/> in <navigation-case>:

<navigation-case>
    <from-action>#{bean.submit}</from-action>
    <from-outcome>success</from-outcome>
    <to-view-id>/page.jsf</to-view-id>
    <redirect/>
</navigation-case>

or by invoking ExternalContext#redirect() in bean's action method:

public void submit() {
    doYourThing();
    FacesContext.getCurrentInstance().getExternalContext().redirect("page.jsf");
}

The only disadvantage is that the redirect implicitly creates a new request, hereby garbaging the initial request including all of its attributes (and thus also all request scoped managed beans and FacesMessages). In some cases it doesn't matter, but in other cases it surely would. I don't do Seam, but if I am correct, they have solved this problem with help of the so-called conversation scope and automatically retaining the FacesMessages through the redirect. You could take benefit of it.

With a4j/RichFaces, use the a4j:queue or assign a queue to the button. That way multiple clicks will get queued and only one will actually go through. The way to set it globally for your app if you're using RichFaces (we did this instead of setting up a queue everywhere), is to put the following in your web.xml:

<context-param>
    <param-name>org.richfaces.queue.global.enabled</param-name>
    <param-value>true</param-value>
</context-param>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!