【发布时间】:2014-01-10 05:54:16
【问题描述】:
在尝试创建 JSF 页面以上传文件时,我在使用带有 Tommahawk Myfaces 的 CDI 时遇到了一些问题。提到这个问题,Tommahawk MyFaces 似乎与 CDI 不兼容,但这是正确的吗?
我的豆子是这样的:
@ManagedBean
@RequestScoped
public class Bean {
private UploadedFile uploadedFile;
public void submit() throws IOException {
String fileName = FilenameUtils.getName(uploadedFile.getName());
String contentType = uploadedFile.getContentType();
byte[] bytes = uploadedFile.getBytes();
// Now you can save bytes in DB (and also content type?)
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(String.format("File '%s' of type '%s' successfully uploaded!", fileName, contentType)));
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
}
但是,当我将 @ManagedBean 和 @RequestScoped 替换为 @Named 或 @Model 时,我会在部署时收到以下警告:
WELD-001529 为没有任何适当构造函数的类 org.apache.myfaces.webapp.filter.TomahawkFacesContextFactory 创建了一个 InjectionTarget 实现。
当我使用以下 JSF 页面上传文件时,当我使用 @Named 或 @Model 时,UploadedFile 的值为 null。但不是@ManagedBean' and@RequestScope. This is the.xhtml`文件,我确实使用tommahawk Myfaces:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:t="http://myfaces.apache.org/tomahawk"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Tomahawk file upload demo</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{bean.uploadedFile}" />
<h:commandButton value="submit" action="#{bean.submit()}" />
<h:messages />
</h:form>
</h:body>
</html>
所以我假设 CDI 不喜欢 tomahawk 库,因为没有默认构造函数?
【问题讨论】:
-
尝试设置扩展过滤器。你可以找到更多关于它的信息Here
-
扩展过滤器已经设置好,因为它与@ManagedBean 一起使用。我只是没有显示它的 XML。
标签: jakarta-ee cdi myfaces