JSF:使用ajax清除inputTextArea时出现怪异的行为 [英] JSF: weird behavior when clear inputTextArea with ajax

查看:173
本文介绍了JSF:使用ajax清除inputTextArea时出现怪异的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我所拥有的

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

因此postMessage()保留数据,然后将注释值设置为空

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

comment.setComment("");

做得好.当我按下按钮时,消息被发布,文本被清除.但是奇怪的是,当我单击刷新"时,该消息重新出现在inputTextArea内部(它不会发布,只是重新出现在文本框中).有办法解决这个问题吗?
P/S:我想要一个ajax解决方案的原因是避免了,在用户单击提交"按钮之后,单击刷新,导致同一条消息将被发布两次.

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.

推荐答案

这是特定于Web浏览器的. Firefox尤其暴露了这种行为.该页面是从浏览器缓存中请求的,所有表单数据都来自浏览器缓存以及最后输入的"数据.

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.

要解决此问题",您想对动态JSF请求禁用浏览器缓存.最简单的方法是创建一个 Filter 注释为@WebFilter(servletNames={"facesServlet"})(其中facesServletFacesServlet<servlet-name>,在web.xml中定义),并且在doFilter()方法中基本上包含以下内容:

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

这篇关于JSF:使用ajax清除inputTextArea时出现怪异的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆