tags:

views:

62

answers:

1

Here is what I have

<h:inputTextarea id="comment" rows="3" cols="50" value="#{bean.comment}" /><br/>
<p:commandButton value="Comment" actionListener="#{bean.postMessage}" update="comment"/>

so postMessage() persist data, then set the the value of comment to empty like this

comment.setComment("");

Work great. When I press the button, message is posted, text is cleared. But what weird is when I click refresh, the message appear back inside the inputTextArea (It does not get posted, just re-appear inside the text box). Is there a way to fix this?
P/S: the reason I want a ajax solution, is to avoid, after the user click the submit button, then hit refresh, result in the same message will get posted twice.

+1  A: 

This is specific to the webbrowser. Among others, Firefox exposes this behaviour. The page is been requested from the browser cache and any form data is coming from the browser cache as well as "last entered" data.

To solve this "problem", you'd like to disable browser cache for dynamic JSF requests. Easiest is to create a Filter which is annotated as @WebFilter(servletNames={"facesServlet"}) (where facesServlet is the <servlet-name> of the FacesServlet as definied in web.xml) and contains basically the following in doFilter() method:

HttpServletResponse hsr = (HttpServletResponse) response;
hsr.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
hsr.setHeader("Pragma", "no-cache"); // HTTP 1.0.
hsr.setDateHeader("Expires", 0); // Proxies.
chain.doFilter(request, response);
BalusC
You are right, Chrome does not behave that way. Does disable Cache reduce the performance? I never used cached before, but always hear how cached can increase performance.
Harry Pham
General consensus is that you'd like to disable caching for *dynamic* content (jsp, jsf, etc) and enable caching for *static* content (img, css, js, etc). More hints can be found in [this article](http://balusc.blogspot.com/2009/09/webapplication-performance-tips-and.html).
BalusC
ohhh yeah. Great stuffs. Definitely read it soon. Thanks man
Harry Pham
You're welcome.
BalusC