當我嘗試創建一個JSF頁面上傳文件時,我遇到了一些使用CDI和Tommahawk Myfaces的問題。參考這個問題,看起來好像Tommahawk MyFaces與CDI不兼容,但這是否正確?Tommahawk Myfaces和CDI
我Bean是這樣的:
@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的InjectionTarget實現是爲沒有任何適當的構造函數的org.apache.myfaces.webapp.filter.TomahawkFacesContextFactory類創建的。
當我上傳文件有以下JSF頁,UploadedFile
值爲空,當我使用@Named
或@Model
。但與@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](http://myfaces.apache.org/tomahawk/extensionsFilter.html) – lu4242
擴展插件過濾器已經設置好了,因爲它和@ManagedBean一起工作。我只是沒有顯示它的XML。 – Kerry