[Ajax4JSF] JSF中对应的HttpServletResponse resp是什么?

king_108 2008-11-24
HttpServletResponse resp = (HttpServletResponse) res;

上面的是 STRUTS中用到的,现在前台使用JSF了,请问上面的CODE 在JSF中对应的是什么?
yourenyouyu2008 2008-11-24
建议你抛弃struts的概念,jsf里你基本上是不需要直接使用HttpServletResponse的。
king_108 2008-11-24
yourenyouyu2008 写道
建议你抛弃struts的概念,jsf里你基本上是不需要直接使用HttpServletResponse的。



我也想啊,原来是STRUTS 中 用 HttpServletResponse 的 输出流去 生成 ZIP 文件的,现在 到了JSF ,发现没这个流
yourenyouyu2008 2008-11-24
public void generateXml(){
chart_index++;
System.out.println("+++++++++++++++"+chart_index+"++++++++++++++++++");
String xmlText="";
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputFormat format = new OutputFormat("  ", true, "UTF-8");
XMLWriter writer = new XMLWriter(out, format);
writer.write(getDocument(searchCriteriaBean,chart_index));
xmlText = out.toString("UTF-8");
} catch (Exception ex) {
ex.printStackTrace();
}
javax.servlet.http.HttpServletResponse response = (HttpServletResponse) javax.faces.context.FacesContext
.getCurrentInstance().getExternalContext().getResponse();
response.reset();
try {
response.getWriter().write(xmlText);
System.out.println("xmlText================="+xmlText);
} catch (IOException e) {

}
javax.faces.context.FacesContext.getCurrentInstance()
.responseComplete();

if(chart_index==8){
System.out.println("set chart_index to 0");
chart_index=0;
}
}
yourenyouyu2008 2008-11-24
只是我们在jsf中输出流的例子
king_108 2008-11-24
HttpServletResponse resp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();					resp.setContentType("application/zip");
			resp.setHeader("Content-Disposition", "attachment; filename="+getZipFilename(mlApp)+";"); 
ZipOutputStream zos = null;
File docCheckListFile = null;
zos = new ZipOutputStream(resp.getOutputStream());


这样解决了
king_108 2008-11-24

HttpServletResponse resp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();					resp.setContentType("application/zip");
resp.setHeader("Content-Disposition", "attachment; filename="+getZipFilename(mlApp)+";"); 
ZipOutputStream zos = null;
File docCheckListFile = null;
zos = new ZipOutputStream(resp.getOutputStream());


huahuashijie888 2008-12-05
我用的也是jsf框架,为什么在jsp页面,我点击文件下载,弹出正常的问文件下载对话框,但是当我执行相应的操作后,再点击页面的保存按钮,会再次出现文件下载对话框,我在文件downFile()中调用了FacesContext.getCurrentInstance().responseComplete();为什么不能结束下载请求?public static void openFile(String fileName,String subDir)
{
try
{
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
if(FacesContext.getCurrentInstance().getResponseComplete())return;
if (!subDir.equals("")) subDir = subDir + "\\";
fileName = request.getRealPath("upload")+"\\" + subDir + fileName;
java.io.File f = new java.io.File(fileName);
if (f.exists()&&f.canRead())
{

response.setContentType ("application/x-msdownload octet-stream;charset=ISO8859-1");
response.setHeader ("Content-Disposition", "attachment;filename=\"" + new String(f.getName().getBytes(), "ISO8859-1") + "\"");
// response.setContentLength((int) f.length());
java.io.BufferedInputStream fileInputStream = new java.io.BufferedInputStream(new java.io.FileInputStream(f));

int i;
// java.io.OutputStream out = response.getOutputStream();
java.io.PrintWriter out = response.getWriter();
// out.clearBuffer();
while ((i=fileInputStream.read()) != -1) out.write(i);
fileInputStream.close();
out.flush();
out.close();
FacesContext.getCurrentInstance().responseComplete();
}

} catch (IOException e) {
e.printStackTrace();
}

// String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
// if (fileType.equals("doc"))
// openWordFile(fileName);
// else if  (fileType.equals("xls"))
// openExcelFile(fileName);
}
songxh2 2008-12-06
FacesContext.getCurrentInstance().responseComplete();这句话只表示输出到此结束,并不能清除以前的输出流,这要搞清楚。文件输出流已经传输给浏览器,浏览器判断http输出head是文件流并提示下载。
huahuashijie888 2008-12-06
songxh2 写道
FacesContext.getCurrentInstance().responseComplete();这句话只表示输出到此结束,并不能清除以前的输出流,这要搞清楚。文件输出流已经传输给浏览器,浏览器判断http输出head是文件流并提示下载。

不是吧?out.close()才表示输出到此结束。FacesContext.getCurrentInstance().responseComplete()表示此次请求的jsf生命周期结束。
Global site tag (gtag.js) - Google Analytics