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

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

问题描述

我正在研究用 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 属性,它在我的后台bean 中调用一个方法,所以问题是我何时刷新页面操作事件获取发生并执行从表中删除另一个资源的方法.提前致谢

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 中,您只需将 faces-redirect=true 参数添加到 bean 操作的结果中即可实现这一点.

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>s,那么添加 到案例.

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以这种方式被垃圾化(重定向基本上指示网络浏览器创建一个全新的请求),因此您无法在请求范围内传递数据以在重定向页面中重新显示它.例如,显示成功消息.在 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天全站免登陆