开发者

JSF: weird behavior when clear inputTextArea with ajax

开发者 https://www.devze.com 2023-01-16 22:58 出处:网络
Here is what I have <h:inputTextarea id=\"comment\" rows=\"3\" cols=\"50\" value=\"#{bean.comment}\" /><br/>

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 a开发者_如何学编程jax solution, is to avoid, after the user click the submit button, then hit refresh, result in the same message will get posted twice.


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);
0

精彩评论

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

关注公众号