Search code examples
jsffile-uploadglassfishcditomahawk

Adding beans.xml Breaks Input File Uploading


I am building a web application that uploads files and am trying to add CDI features that require the use of a beans.xml file. The problem is that when I add a beans.xml file to the webapp it breaks the previously functional input file uploading, even before I switch over to any features that require beans.xml. Why is even the presence of a beans.xml file breaking this feature?

Environment and Dependencies:
JDK 1.7.0_45
Netbeans IDE 7.4 Patch 2
Windows 7 version 6.1 running on x86
javaee-web-api-6.0
primefaces-3.5
tomahawk-1.1.14
GlassFish Server 4.0

Here's the code if that will help:

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>

Web Page (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>

Solution

  • GlassFish 4 ships with JSF 2.2 which has already a native file upload support in flavor of <h:inputFile>. The FacesServlet has already processed the uploaded file before it hits Tomahawk's <t:inputFileUpload>.

    You have basically 2 options:

    1. Get rid of Tomahawk and use native file upload component <h:inputFile> instead.

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

      with

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

    2. Downgrade from JSF 2.2 to JSF 2.1 or 2.0. Downgrading the whole server as you did is one way, albeit somewhat clumsy.