Jsf 添加beans.xml会中断输入文件的上载
我正在构建一个上传文件的web应用程序,并尝试添加需要使用beans.xml文件的CDI功能。问题是,当我将beans.xml文件添加到webapp时,甚至在我切换到任何需要beans.xml的功能之前,它就破坏了以前上传的功能性输入文件。为什么即使存在beans.xml文件也会破坏这一特性 环境和依赖关系:Jsf 添加beans.xml会中断输入文件的上载,jsf,file-upload,glassfish,cdi,tomahawk,Jsf,File Upload,Glassfish,Cdi,Tomahawk,我正在构建一个上传文件的web应用程序,并尝试添加需要使用beans.xml文件的CDI功能。问题是,当我将beans.xml文件添加到webapp时,甚至在我切换到任何需要beans.xml的功能之前,它就破坏了以前上传的功能性输入文件。为什么即使存在beans.xml文件也会破坏这一特性 环境和依赖关系: JDK 1.7.0_45 Netbeans IDE 7.4修补程序2 在x86上运行的Windows 7 6.1版 javaee-web-api-6.0 primefaces-3.5 战斧
JDK 1.7.0_45
Netbeans IDE 7.4修补程序2
在x86上运行的Windows 7 6.1版
javaee-web-api-6.0
primefaces-3.5
战斧-1.1.14
GlassFish服务器4.0 下面是代码,如果这有帮助的话: beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
faces-config.xml
<?xml version="1.0" encoding="utf-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
</faces-config>
我最终切换到Glassfish服务器3.1.2.2,问题得到了解决。Glassfish 4附带了JSF 2.2,JSF 2.2已经提供了
风格的本地文件上传支持。FacesServlet
在上传的文件到达战斧
之前已经对其进行了处理
您基本上有两种选择:
<h:form enctype="multipart/form-data">
<h:inputFile value="#{bean.uploadedFile}" />
...
</h:form>
成功了!令人困惑的是,我发布的代码实际上是在我的一位同事的环境中工作的,尽管JSF/Tomahawk之间存在明显的冲突。因此,我认为这是一个环境问题,但感谢你抓住了问题的核心。
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.apache.myfaces.custom.fileupload.UploadedFile;
@SessionScoped
@ManagedBean(name = "fileUploadController")
public class FileUploadController implements Serializable {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
if(file != null) {
FacesMessage msg = new FacesMessage("Succesful! ", file.getName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
else {
FacesMessage msg = new FacesMessage("Failure to upload.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
</faces-config>
<h:form enctype="multipart/form-data">
<h:inputFile value="#{bean.uploadedFile}" />
...
</h:form>
import javax.servlet.http.Part;
// ...
private Part uploadedFile; // +getter +setter