刷新页面时,如何避免重新执行上一次表单提交操作? [英] How to avoid re-execution of last form submit action when the page is refreshed?

查看:301
本文介绍了刷新页面时,如何避免重新执行上一次表单提交操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究用JSF开发的项目.每当我们刷新JSF页面时,都会重新执行最后一个动作事件.例如,当我提交表单以删除列表的条目并刷新结果页面时,列表中相同位置的另一个条目也将被删除.这是怎么引起的,我该如何解决?

I am working on project which is developed in JSF. Whenever we are refreshing the JSF page, then the last action event is re-executed. For example, when I submit the form to delete an entry of a list and refresh the result page, then another entry from the list at the same position is deleted as well. How is this caused and how can I solve it?

我已经尝试过faces-config.xml,但这不能解决我的问题,

i have tried in faces-config.xml but that does not solve my problem,

要弄清楚我面临的问题,是我是CommandLink从数据表中删除一个资源,我正在使用actionlistener属性,该属性在backingbean中调用一个方法,所以问题是我何时刷新页面操作事件获取发生并执行从表中删除另一个资源的方法.预先感谢

To get more clear on Problem i am facing is that i am commandLink to remove one resource from datatable ,i am using actionlistener attribute which calls one method in my backingbean,so problem is when ever i am refreshing the page action event getting occured and method is executed which remove another resource from the table . Thanks in advance

推荐答案

症状表明该页面是POST请求所请求的,并且您忽略了Web浏览器的警告,即刷新请求时将重新发送数据.刷新POST请求当然会导致它被重新执行.这不是JSF特定的问题.

The symptoms indicate that the page was requested by a POST request and that you're ignoring the webbrowser's warning that the data will be resent when refreshing the request. Refreshing a POST request will of course result in it being re-executed. This is not a JSF specific problem.

通常的解决方案是在执行POST请求后将重定向发送到GET请求.这样,客户端最终将在浏览器视图中收到GET请求.刷新后将仅重新执行不会(不应)修改任何内容的GET请求(除非您在与视图关联的请求范围的Bean的构造函数中执行此操作).这也称为 POST-Redirect-GET 模式.

The common solution to that is to send a redirect to a GET request after executing the POST request. This way the client will end up having the GET request in the browser view. Refreshing this will then only re-execute the GET request which doesn't (shouldn't) modify anything (unless you're doing this in the constructor of a request scoped bean associated with the view). This is also known as the POST-Redirect-GET pattern.

使用JSF 2.0,您只需在bean操作的结果中添加faces-redirect=true参数即可实现这一目标.

With JSF 2.0, you can achieve this by simply adding faces-redirect=true parameter to the bean action's outcome.

public String submit() {
    // ...

    return "viewid?faces-redirect=true";
}

如果您仍在faces-config.xml中使用老式的<navigation-case>,则可以在案例中添加<redirect/>来达到相同的效果.

If you're still using old fashioned <navigation-case>s in faces-config.xml, then the same effect can be achieved by adding <redirect/> to the case.

唯一的缺点是,以这种方式对请求范围的Bean进行了垃圾处理(重定向基本上指示Web浏览器创建一个全新的请求),因此,您不能在请求范围内传递数据以在重定向页面中重新显示它.例如,显示成功消息.在JSF 2.0中,您可以为此使用Flash作用域,或者只通过<f:ajax>提交而不是常规提交来进行POST.

The only disadvantage is that request scoped beans are garbaged this way (a redirect basically instructs the webbrowser to create a brand new request) and thus you cannot pass data in the request scope in order to redisplay it in the redirected page. For example, displaying a success message. In JSF 2.0 you could instead use the flash scope for this or to just let the POST take place by <f:ajax> submit instead of a normal submit.

这篇关于刷新页面时,如何避免重新执行上一次表单提交操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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