添加beans.xml中断输入文件上传

时间:2014-01-08 16:20:57

标签: 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
战斧-1.1.14
GlassFish Server 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>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.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-app_3_0.xsd" >
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>  
    <param-name>javax.faces.CONFIG-FILES</param-name>  
    <param-value>WEB-INF/faces-config.xml</param-value>  
</context-param> 
<context-param>  
    <param-name>com.sun.faces.expressionFactory</param-name>  
    <param-value>com.sun.el.ExpressionFactoryImpl</param-value>  
</context-param>  

<filter>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>login.jsf</welcome-file>
</welcome-file-list>
<error-page>
    <error-code>401</error-code>
    <location>/WEB-INF/errorpages/unauthorized.xhtml</location>
</error-page>
</web-app>

网页(login.xhtml)

<?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:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:t="http://myfaces.apache.org/tomahawk"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
    <title>Input File Test</title>
    <c:set value="${facesContext.externalContext.requestContextPath}" var="path" scope="view"/>
    <link href="${path}/css/default.css" rel="stylesheet" type="text/css"/>
</h:head>
<h:body>
    <div id="main">
        <h:form enctype="multipart/form-data">
            <p:panel header="Submit Object Relational Model">
                <h:panelGrid columns="2" >


                    <t:inputFileUpload id="file" name="path" value="#{fileUploadController.file}"/>
                    <f:facet name="footer">
                        <h:commandButton value="Submit" action="#{fileUploadController.upload()}"/>
                    </f:facet>

                </h:panelGrid>
                <p:messages showDetail="true"/>
            </p:panel>
        </h:form>
    </div>
</h:body>
</html>

Bean(FileUploadController.java)

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);
    }
}
}

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>

2 个答案:

答案 0 :(得分:0)

我最终切换到Glassfish Server 3.1.2.2,问题解决了。

答案 1 :(得分:0)

GlassFish 4附带JSF 2.2,它已经具有<h:inputFile>风格的本机文件上载支持。 FacesServlet已经处理了已上传的文件,然后才能击中Tomahawk的<t:inputFileUpload>

你基本上有两个选择:

  1. 摆脱Tomahawk并改为使用原生文件上传组件<h:inputFile>

    <h:form enctype="multipart/form-data">
        <h:inputFile value="#{bean.uploadedFile}" />
        ...
    </h:form>
    

    import javax.servlet.http.Part;
    
    // ...
    
    private Part uploadedFile; // +getter +setter
    

  2. 从JSF 2.2降级到JSF 2.1或2.0。像你一样降级整个服务器只是一种方式,虽然有些笨拙。