开发者

JSF, actionlistener at facelets

开发者 https://www.devze.com 2022-12-28 05:18 出处:网络
I\'m using JSF (Mojarra 1.2) with Richfaces (3.3.2) within some facelets which are used as portlets (I\'m using the Jboss Portlet Bridge 2.0 here). Now I\'m fac开发者_运维技巧ing something strange: I\

I'm using JSF (Mojarra 1.2) with Richfaces (3.3.2) within some facelets which are used as portlets (I'm using the Jboss Portlet Bridge 2.0 here). Now I'm fac开发者_运维技巧ing something strange: I've got an actionlistener on my <h:commandButton> which is triggered, when the button is clicked but when I simply reload the page, the action is executed everytime I load the page again. This happens only if I already triggered the action before. Is this behaviour normal?

I should notice that Spring 2.5 is used to manage my beans, the mentioned beans are session-scope beans, maybe this is a interessting point?!


Yes, reloading a HTTP POST request will execute the HTTP POST request again and thus trigger all associated server-side actions again. This issue affects all webapplications in general and is not per se related to JSF.

A well known fix to this is the POST-Redirect-GET (PRG) pattern. Basically you need to redirect the POST request to a GET request immediately after processing the action, so that the result page will be delivered by a HTTP GET request. Refreshing this HTTP GET request won't execute the initial HTTP POST request anymore.

This pattern has however one caveat: since it concerns a brand new request, all request scoped beans are garbaged and renewed in the new request. So if you'd like to retain the data in the new request, you would need to either pass them as GET parameters or to store it in the session scope. Usually just reloading the data in bean's constructor is sufficient. But since you mention to use session scoped beans only (which is however not the best practice, but this aside), this shouldn't be a big concern for you.

Turning on PRG in JSF is relatively easy, just add the following entry to the associated <navigation-case>:

<redirect />

Or if you prefer to fire it programmatically, then make use of ExternalContext#redirect() in the bean's action method:

public void submit(ActionEvent event) {
    // ...
    FacesContext.getCurrentInstance().getExternalContext().redirect(someURL);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号