JSF, actionlistener in facelets
I am using JSF (Mojarra 1.2) with Richfaces (3.3.2) inside some of the chips that are used as portlets (here I am using Jboss Portlet Bridge 2.0). Now I'm running into something weird: I have one actionlistener
on mine <h:commandButton>
that fires when the button is clicked, but when I just reload the page, the action is executed every time I load the page again. This only happens if I have already called the action before. Is this normal behavior?
I should note that Spring 2.5 is used to manage my beans, the beans mentioned are session scope beans, maybe this is the point of interest ?!
a source to share
Yes, reloading the HTTP POST request will execute the HTTP POST request again and thus trigger all associated server side actions again. This issue affects all web applications in general and is not related to JSF itself.
It is well known to be the POST-Redirect-GET (PRG) pattern . Basically, you need to redirect the POST request to the GET request immediately after processing the action so that the results page is delivered on the HTTP GET request. Updating this HTTP GET request will no longer do the initial HTTP POST request.
However, this pattern has one caveat: since it refers to a new request, the entire request beans are loaded and updated in the new request. Therefore, if you want to store the data in a new request, you either need to pass it as GET parameters or store it in the session scope. It is usually sufficient to simply reload the data in the bean's constructor. But since you mention that using only limited session beans (but that's not the best practice, but that's aside), that shouldn't worry you too much.
Including PRG in JSF is relatively easy, just add the following entry to the linked one <navigation-case>
:
<redirect />
Or, if you prefer to run it programmatically, use a ExternalContext#redirect()
bean in your action method:
public void submit(ActionEvent event) {
// ...
FacesContext.getCurrentInstance().getExternalContext().redirect(someURL);
}
a source to share