Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

jsf 2 - How to avoid re-execution of last form submit action when the page is refreshed?

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?

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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";
}

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...