JSF: weird behavior when clear inputTextArea with ajax

That's what i

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

so postMessage()save the data then set the comment value to empty like this

comment.setComment("");

Work great. When I press the button, the message is sent, the text is cleared. But what is strange is that when I click the Refresh button, the message appears inside inputTextArea( It is not published, it just appears again in the text box ). Is there any way to fix this?
P / S: the reason I want to use the ajax solution is to avoid, after the user clicks the submit button, then click "Refresh", as a result , the same message will be sent twice .

+3
source share
1 answer

This applies to a web browser. In particular, Firefox reveals this behavior. The page is requested from the browser cache, and any form data comes from the browser cache, as well as the "last entered" data.

To solve this "problem", you want to disable the browser cache for dynamic JSF requests. The easiest way is to create Filterone that is annotated as @WebFilter(servletNames={"facesServlet"})(where facesServletis <servlet-name> facesServletas defined in web.xml) and contains basically the following in a method doFilter():

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);
+4
source

Source: https://habr.com/ru/post/1765146/


All Articles