JSF2 PRG和验证错误 [英] JSF2 PRG and Validation Error

查看:100
本文介绍了JSF2 PRG和验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有在JSF2中找到PRG模式的明确实现.
BalusC博客提供了一个很好的解决方案,但作者本人指出,这种解决方案不适用于JSF2.
基于Flash作用域的解决方案如果没有发生验证错误,但在验证的情况下可以正常工作错误,因为生命周期未调用NavigationHandler,所以未执行REDIRECT.假定浏览器未缓存页面(在HTTP响应中设置正确的标题),如果发生验证错误并且用户单击重新加载"浏览器按钮,则将执行POST,而不是GET.
实现鲁棒的PRG模式(即使使用JSF2验证错误也可以使用)的最佳实践是什么?

I haven't still found a definitive implementation of PRG pattern with JSF2.
The BalusC blog presents a very good solution, but as the author himself states, such a solution does not apply for JSF2.
The solutions based on Flash scope work fine if no validations error occurs, but in case of validation error the REDIRECT is not executed because the lifecycle does not call the NavigationHandler. Assuming that pages are not cached by the browser (setting the right headers in the HTTP response), if a validation error occurs and the user clicks the "reload" browser button then a POST is executed, not a GET.
What's the best practice for implementing a robust PRG pattern which works even on validation errors using JSF2?

推荐答案

我正面临着同样的挑战.找不到100%令人满意的解决方案.但是请注意来自BalusC的方法.如果在没有浏览器缓存控制的情况下按下浏览器的后退"按钮,此解决方案将不会保留值.

I'm facing the same challenge. Did not find a 100% satisfying solution. But look to this approach from BalusC. This solution will not preserve values if hitting the browsers back button without browser cache control.

<h:form>
  <h:inputText id="somefield" required="true" value="#{formbean.someProperty}"/>
  <h:message for="somefield" />
  <h:commandButton value="POST with redirect and ajax" action="third?faces-redirect=true">
    <f:ajax execute="@form" render="@form" />
  </h:commandButton>
</h:form>

加载页面,将字段保留为空,发布表单(出现->错误消息),输入数据,再次发布,然后单击后退"按钮.这将再次显示该表单,错误消息消失了,但输入字段值也消失了.

Load the page, leave the field empty, post the form (-> error message appears), enter data, post again and now hit the back button. This will show the form again, the error message has gone but the input field value has also gone.

假设您以某种方式保留了formbeans值(会话范围,使用flash,view-params等),则可以在单击后退按钮时禁用浏览器缓存,从而导致页面重新加载(要求GET).我使用以下代码在每页上执行此操作:

Assuming you preserve the formbeans value somehow (session scope, using the flash, view-params, ...), you could disable the browsers cache causing a page reload (GET-requested) when hitting the back button. I did this per page with the following code:

JSF页面:

<f:view>
  <f:event type="preRenderView" listener="#{formbean.doNotCache}"/>
</f:view>

Bean:

public void doNotCache(ComponentSystemEvent event) {
  // Using Omnifaces to simplify code
  Faces.getResponse().setHeader("Pragma", "no-cache");
  Faces.getResponse().setHeader("Cache-Control", "no-cache");
  Faces.getResponse().addHeader("Cache-Control", "must-revalidate");
  Faces.getResponse().addHeader("Cache-Control", "no-store");
}

我将看看该解决方案是否可以在现实世界中使用.在我看来,它们应该是验证系统的配置选项,以便随后使用Post-Redirect-Get-Pattern.它应该重定向(而不是转发)无效的表单. (可能不是一项琐碎的任务,例如,如何处理/重新显示无效的字段?).也许我错了,但感觉好像缺少类似的选择.

I will see if this solution holds on real world sites. In my opinion their should be a configuration option for the validation system offering a consequent use of the Post-Redirect-Get-Pattern. It should redirect (not forward) invalid forms. (Probably not a trivial task, for example howto handle/redisplay invalid fields?). Perhaps I'm wrong but it feels like a similar option is missing.

这篇关于JSF2 PRG和验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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