如何解决下载内容后无法清除jsf参数
当我下载内容时,单击primefaces commandButton,我设置了一个jsf参数,该参数可用于必需的字段验证。 下载完成后,如果我清除了一些必填字段值,然后单击另一个不传递jsf参数的commandButton来触发验证,则会触发验证,验证失败。 这里是示例jsf代码:
<p:inputTextarea value="#{flowScope.inputText}" required="#{not empty param.includeInSave)}" requiredMessage="Required Input Text" />
<p:commandButton id="add_table_row" value="Call action without validation" action="add_table_row" update=":table_pnl" />
<p:commandButton id="download_pdf" value="Download" action="download_pdf" ajax="false" onclick="if(!confirm('Start download?')) return false;">
<f:param name="includeInSave" value="true" />
<p:commandButton id="save" value="Save" action="save" ajax="false" onclick="if(!confirm('Save?')) return false;">
<f:param name="includeInSave" value="true" />
</p:commandButton>
因此,当我单击“ download_pdf”时,将触发验证并且验证有效。 清除inputTextarea值并单击“ add_table_row”命令按钮后,该操作仍在请求中包含includeInSave参数,因为再次触发了验证si。 如果单击“保存” commandButton,则不会发生此事,这在jsf代码中是相同的,但不会在Java代码中下载文件。 所以我认为我必须在Java的HttpServletResponse中设置一些区别
在Java代码中,我将以这种方式下载文件:
public static Boolean downloadBytes(byte[] bytes,String name,String mimeType,RequestContext requestContext) {
HttpServletResponse response = (HttpServletResponse) requestContext.getExternalContext().getNativeResponse();
response.reset();
response.setContentType(mimeType);
response.setHeader("Content-disposition","attachment; filename=" + name);
response.setHeader("Content-Length",Integer.toString(bytes.length));
response.setHeader("Cache-Control","no-cache,no-store,must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma","no-cache"); // HTTP 1.0.
response.setDateHeader("Expires",0);
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
int size = 4096;
int i = 0;
while(i < bytes.length)
{
int max_range = Math.min(i + size,bytes.length);
byte[] mini_buf = Arrays.copyOfRange(bytes,i,max_range);
i+= size;
outputStream.write(mini_buf);
}
FacesContext.getCurrentInstance().responseComplete();
return Boolean.TRUE;
} catch (Exception e) {
return Boolean.FALSE;
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException err) {
err.printStackTrace();
}
}
}
我希望有人能帮助我。谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。