使用 Tomahawk t:inputFileUpload 时未调用 CommandButton 操作
我将 JSF 2.1 与 JSP 文件一起使用。我尝试将 JSF 2.1 与 Facelet 文件一起使用,但没有使 Tomahawk(MyFaces 扩展)正常工作。
我正在尝试使用 Tomahawk 上传文件。我的表单如下所示:
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>FILE UPLOAD</title>
</head>
<body>
<fieldset>
<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{fileUploadBean.upFile}"
size="25"
required="true" /><br/>
<h:commandButton value="Validar" action="#{fileUploadBean.upload}" />
</h:form>
</fieldset>
</body>
</html>
</f:view>
我的 bean 是(因为我使用的是 JSF 2.1,所以我不需要使用 faces-config.xml
并且我使用注释):
import java.io.IOException;
import java.io.InputStream;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.myfaces.custom.fileupload.UploadedFile;
/**
*
* @author federico.martinez
*/
@ManagedBean
@RequestScoped
public class FileUploadBean {
private UploadedFile upFile;
public UploadedFile getUpFile(){
return upFile;
}
public void setUpFile(UploadedFile upFile){
this.upFile = upFile;
}
public String upload(){
InputStream is = null;
try {
is = upFile.getInputStream();
long size = upFile.getSize();
byte[] buffer = new byte[(int)size];
is.read(buffer,0,(int)size);
is.close();
System.out.println("File Upload Successful.");
return "processing-file";
} catch (IOException ex) {
Logger.getLogger(FileUploadBean.class.getName()).log(Level.SEVERE, null, ex);
return "error-lf";
} finally {
try {
is.close();
} catch (IOException ex) {
Logger.getLogger(FileUploadBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
好吧,就是这样,但最后问题是当我单击 h:commandButton 时,它没有执行任何操作。看来我的 bean 的“上传”方法不起作用或者什么的。我缺少什么?
I'm using JSF 2.1 with JSP files. I tried to use JSF 2.1 with Facelet files, but didn't get Tomahawk (MyFaces extension) to work.
I'm trying to upload a file using Tomahawk. My form looks like follows:
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>FILE UPLOAD</title>
</head>
<body>
<fieldset>
<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{fileUploadBean.upFile}"
size="25"
required="true" /><br/>
<h:commandButton value="Validar" action="#{fileUploadBean.upload}" />
</h:form>
</fieldset>
</body>
</html>
</f:view>
and my bean is (as I'm using JSF 2.1, I don't need to use faces-config.xml
and I use annotations):
import java.io.IOException;
import java.io.InputStream;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.apache.myfaces.custom.fileupload.UploadedFile;
/**
*
* @author federico.martinez
*/
@ManagedBean
@RequestScoped
public class FileUploadBean {
private UploadedFile upFile;
public UploadedFile getUpFile(){
return upFile;
}
public void setUpFile(UploadedFile upFile){
this.upFile = upFile;
}
public String upload(){
InputStream is = null;
try {
is = upFile.getInputStream();
long size = upFile.getSize();
byte[] buffer = new byte[(int)size];
is.read(buffer,0,(int)size);
is.close();
System.out.println("File Upload Successful.");
return "processing-file";
} catch (IOException ex) {
Logger.getLogger(FileUploadBean.class.getName()).log(Level.SEVERE, null, ex);
return "error-lf";
} finally {
try {
is.close();
} catch (IOException ex) {
Logger.getLogger(FileUploadBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Well that's it, but finally the problem is when I click on h:commandButton, it doesn't do anything. It seems like the method "upload" from my bean isn't working or something. what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Tomahawk 文档中所述,您需要注册
ExtensionsFilter
在web.xml
中。它负责解析multipart/form-data
请求并将多部分表单数据部分转换为正常的请求参数和属性,以便FacesServlet
可以继续使用它们。如果您不注册ExtensionsFilter
,则FacesServlet
将无法执行任何操作,因为没有请求参数来确定要更新的模型值和要执行的操作被调用。另请参阅:
与具体问题无关,您确实应该考虑使用 Facelets 而不是 JSP。它比 JSP 提供了很多优势,例如 模板化、复合组件和ajax 支持。
As stated in the Tomahawk documentation, you need to register
ExtensionsFilter
inweb.xml
. It's the one responsible for parsingmultipart/form-data
requests and converting the multipart form data parts to normal request parameters and attributes, so that theFacesServlet
can continue using them. If you don't register theExtensionsFilter
, then theFacesServlet
can't do anything because there are no request parameters in order to determine the model values to be updated and actions to be invoked.See also:
Unrelated to the concrete problem, you should really consider using Facelets instead of JSP. It offers so many advantages over JSP, such as templating, composite components and ajax support.