纯Java/JSF实现可防止双重提交 [英] Pure Java/JSF implementation for double submit prevention

查看:168
本文介绍了纯Java/JSF实现可防止双重提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在带有多个组件库PrimeFaces 4.0,Tomahawk 2.0,RichFaces等的WebSphere v8.5上使用JSF 2.0.

We're using JSF 2.0 on WebSphere v8.5 with several component libraries PrimeFaces 4.0, Tomahawk 2.0, RichFaces, etc.

我正在寻找一种通用机制,以避免刷新页面或再次单击提交按钮时重新提交表单.我有许多具有不同方案的应用程序.

I am looking for generic mechanism to avoid form re-submission when the page is refreshed, or when the submit button is clicked once again. I have many applications with different scenarios.

目前,我已经考虑在onclick属性中使用一段JavaScript禁用按钮,但这并不令人满意.我正在为此目的寻找纯Java实现,例如Struts2 <s:token>.

For now I have considered disabling the button with a piece of JavaScript in onclick attribute, but this is not satisfying. I'm looking for a pure Java implementation for this purpose, something like the Struts2 <s:token>.

推荐答案

我正在寻找一种通用机制,以避免刷新页面时重新提交表单

为此,至少有2种解决方案无法组合:

For that there are at least 2 solutions which can not be combined:

  1. 在同步发布后执行重定向.这样,刷新将仅重新执行重定向的GET请求,而不是初始请求.缺点:您无法再使用请求范围来向最终用户提供任何反馈. JSF 2.0通过提供新的 Flash作用域解决了这一问题.另请参见如何在重定向页面中显示面孔消息.

  1. Perform a redirect after synchronous post. This way the refresh would only re-execute the redirected GET request instead of the initial request. Disadvantage: you can't make use of the request scope anymore to provide any feedback to the enduser. JSF 2.0 has solved this by offering the new flash scope. See also How to show faces message in the redirected page.

在后台异步执行POST(使用Ajax).这样,刷新将仅重新执行打开表单的初始GET请求.您只需要确保这些表单最初仅通过GET请求打开,即您绝不应该通过POST执行页面到页面的导航(无论如何,这本身已经是一个糟糕的设计).另请参见何时应使用h:outputLink代替h:commandLink?

Perform the POST asynchronously in the background (using ajax). This way the refresh would only re-execute the initial GET request which opened the form. You only need to make sure that those forms are initially opened by a GET request only, i.e. you should never perform page-to-page navigation by POST (which is at its own already a bad design anyway). See also When should I use h:outputLink instead of h:commandLink?


或再次单击提交"按钮

为此,基本上也至少有2个解决方案,可以在必要时进行组合:

For that there are basically also at least 2 solutions, which could if necessary be combined:

  1. 仅阻止最终用户在提交过程中和/或成功提交后按下提交按钮.有多种方法,具体取决于具体的功能和设计要求.您可以在提交期间使用JavaScript禁用按钮.您可以使用JSF的disabledrendered属性在提交后禁用或隐藏按钮.另请参见如何在JSF 2中进行防止双击一个>.您也可以在处理ajax请求期间使用覆盖窗口来阻止任何最终用户交互. PrimeFaces为此使用<p:blockUI>.

  1. Just block the enduser from being able to press the submit button during the submit and/or after successful submit. There are various ways for this, all depending on the concrete functional and design requirements. You can use JavaScript to disable the button during submit. You can use JSF's disabled or rendered attributes to disable or hide the button after submit. See also How to do double-click prevention in JSF 2. You can also use an overlay window during processing ajax requests to block any enduser interaction. PrimeFaces has <p:blockUI> for the purpose.

在服务器端验证新添加的实体的唯一性.如果您绝对希望避免重复是出于技术原因而不是出于功能原因,那么这样做会更加健壮.这非常简单:在相关的DB列上放置UNIQUE约束.如果违反了此约束,则数据库(和DB交互框架,如JPA)将引发约束违反异常.最好与自定义JSF验证器结合使用,该验证器通过在确切的列上执行SELECT并检查是否未返回记录来预先验证输入. JSF验证器允许您以友好的面孔消息的形式显示问题.另请参见针对DB验证电子邮件格式和唯一性.

Validate uniqueness of the newly added entity in the server side. This is way much more robust if you absolutely want to avoid duplication for technical reasons rather than for functional reasons. It's fairly simple: put a UNIQUE constraint on the DB column in question. If this constraint is violated, then the DB (and DB interaction framework like JPA) will throw a constraint violation exception. This is best to be done in combination with a custom JSF validator which validates the input beforehand by performing a SELECT on exactly that column and checking if no record is returned. A JSF validator allows you to display the problem in flavor of a friendly faces message. See also among others Validate email format and uniqueness against DB.

这篇关于纯Java/JSF实现可防止双重提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆