有时,使用时<h:commandLink>,<h:commandButton>或者<f:ajax>,在action,actionListener或listener与标签相关的方法根本不被调用.或者,bean属性不会使用提交的UIInput值进行更新.
有什么可能的原因和解决方案?
有没有办法从JSF支持bean动作方法提供文件下载?我尝试过很多东西.主要问题是我无法弄清楚如何获取OutputStream响应以便将文件内容写入.我知道如何使用a Servlet,但这不能从JSF表单调用,需要新的请求.
如何OutputStream从当前获得响应FacesContext?
哪个是使用JSF下载文件的正确方法?只是放一个文件链接?在那种情况下我如何获得文件URL?
我见过一个使用BufferedInputStream的例子:
http://www.winstonprakash.com/articles/jsf/file_download_link.htm
有什么区别?
谢谢
我需要知道从servlet访问JSF managedBean(定义了应用程序范围)的最佳方法是什么.目前我的servlet中有这样的东西:
MyApplicationScopeBean bean = null;
try {
FacesContext fContext = FacesUtil.getFacesContext(req, resp);
ServletContext sc = (ServletContext) fContext.getExternalContext().getContext();
bean = (MyApplicationScopeBean) sc.getAttribute("myManagedBean");
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
FacesUtil.java(如http://balusc.blogspot.com/2006/06/communication-in-jsf.html中所述):
import javax.faces.FactoryFinder;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FacesUtil {
// Getters -----------------------------------------------------------------------------------
public static FacesContext getFacesContext(
HttpServletRequest request, HttpServletResponse response)
{
// Get current FacesContext.
FacesContext facesContext = FacesContext.getCurrentInstance();
// Check current FacesContext.
if (facesContext == null) …Run Code Online (Sandbox Code Playgroud) 我正在将dataTable(绑定dataList)导出到excel文件,调用以下方法 xlsWorkBookPrepare("c:\\export.xls");
方法的一部分:
public void xlsWorkBookPrepare(String file) throws IOException
{
/* prepare of workbook */
Workbook wb = new HSSFWorkbook();
Map<String, CellStyle> styles = Style.createStyles(wb);
...
for (FoodList item : dataList)
{
...
}
/* create file */
FileOutputStream fileOut;
try
{
fileOut = new FileOutputStream(file);
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
但路径与服务器有关.如何将它保存在客户端?
解决方案(基于Rangi Lin的回答):
HttpServletResponse res = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
res.setContentType("application/vnd.ms-excel");
res.setHeader("Content-disposition", "attachment; filename=test.xls");
try
{
ServletOutputStream fileOut = res.getOutputStream();
wb.write(fileOut);
fileOut.flush();
fileOut.close();
}
catch …Run Code Online (Sandbox Code Playgroud) jsf ×5
download ×2
java ×2
jsf-2 ×2
action ×1
apache-poi ×1
commandlink ×1
javabeans ×1
servlets ×1