h:commandButton 未调用操作方法
h:commandButton action method is not invoked
我有以下按钮:
<h:commandButton value="Download" action="#{listFiles.downloadFile}" />
以及以下操作方法:
public void downloadFile()
{
// Some code.
}
但是当我按下按钮时没有任何反应。未调用操作方法。
在服务器日志中我看到:
The form component needs to have a UIForm in its ancestry. Suggestion: enclose the necessary components within <h:form>
这对我有用
文件夹路径中我的文件
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;
}
我有以下按钮:
<h:commandButton value="Download" action="#{listFiles.downloadFile}" />
以及以下操作方法:
public void downloadFile()
{
// Some code.
}
但是当我按下按钮时没有任何反应。未调用操作方法。
在服务器日志中我看到:
The form component needs to have a UIForm in its ancestry. Suggestion: enclose the necessary components within
<h:form>
这对我有用
文件夹路径中我的文件
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;
}