JSF: weird behavior when clear inputTextArea with ajax
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?
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);
精彩评论