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

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

问题描述

我们在 WebSphere v8.5 上使用 JSF 2.0 和几个组件库 PrimeFaces 4.0、Tomahawk 2.0、RichFaces 等.

我正在寻找通用机制来避免在页面刷新或再次单击提交按钮时重新提交表单.我有很多不同场景的应用.

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

解决方案

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

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

  1. 在同步发布后执行重定向.这样刷新只会重新执行重定向的 GET 请求而不是初始请求.缺点:您不能再利用请求范围向最终用户提供任何反馈.JSF 2.0 通过提供新的 flash 范围 解决了这个问题.另请参阅如何在重定向页面中显示人脸消息.

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

<小时><块引用>

或者再次点击提交按钮时

为此,基本上也有至少 2 个解决方案,如有必要,可以将它们组合起来:

  1. 只需阻止最终用户在提交期间和/或成功提交后按下提交按钮即可.有多种方法可以做到这一点,一切都取决于具体的功能和设计要求.您可以使用 JavaScript 在提交期间禁用该按钮.您可以使用 JSF 的 disabledrendered 属性在提交后禁用或隐藏按钮.另请参阅如何在 JSF 2 中进行双击预防.您还可以在处理 ajax 请求期间使用覆盖窗口来阻止任何最终用户交互.PrimeFaces 具有 用于此目的.

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

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.

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>.

解决方案

I am looking for generic mechanism to avoid form re-submission when the page is refreshed

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

  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.

  2. 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?


or when the submit button is clicked once again

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

  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.

  2. 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天全站免登陆