我有以下按钮:
<h:commandButton value="Download" action="#{listFiles.downloadFile}" />
以下行动方法:
public void downloadFile()
{
// Some code.
}
但是当我按下按钮时没有任何反应。不会调用action方法。
在服务器日志中,我看到:
表单组件需要在其祖先中具有UIForm。建议:在
中包含必要的组件<h:form>
答案 0 :(得分:1)
这项工作对我来说
我在文件夹路径中的文件
WEB-INF\pages\forms
xhtml
档案中的鳕鱼
<h:commandLink value="Download" action="#{formBean.downloadFile('FileName.doc')}" styleClass="link"/>
豆
public String downloadFile(String fileName) {
try {
FacesContext facesContext = FacesContext.getCurrentInstance();
HttpServletResponse response = ((HttpServletResponse) facesContext.getExternalContext().getResponse());
String reportPath = facesContext.getExternalContext().getRealPath("\\pages\\forms") + File.separator + fileName;
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName+ "\"");
File file = new File(reportPath);
BufferedInputStream bIn = new BufferedInputStream(new FileInputStream(file));
int rLength = -1;
byte[] buffer = new byte[1000];
while ((rLength = bIn.read(buffer, 0, 100)) != -1) {
response.getOutputStream().write(buffer, 0, rLength);
}
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}