1

The application need to import excel files (.xlsx) to populate one database table. The problem comes when I try to upload the file through AJAX. The validator is never called (and the listener never too). When I try to upload the file, a error is thrown from JSF AJAX JavaScript file and show in the browser's console: TypeError: partialResponse is not defined.

My first thought was this is a bug, but i'm not sure. It's my first JSF project.

<h:form id="travels-form" class="toolbar ui form" enctype="multipart/form-data">
    <div class="five wide field">
        <input type="text" id="search" placeholder="Buscar..." />
    </div>
    <div class="actions">
        <div class="inline field wide">
            <h:selectOneMenu valueChangeListener="#{travelBean.filter}" >
                <f:selectItem itemValue="" itemLabel="Todas las placas"  />
                <f:selectItems value="#{travelBean.licensePlates}" var="plate"
                               itemValue="${plate}" itemLabel="${plate}" />
                <f:ajax execute="@this" render=":tblTravels" />
            </h:selectOneMenu>
        </div>
        <h:outputLabel for="excel" value="Excel" styleClass="ui positive button" />
        <h:inputFile id="excel" value="#{excelImporterBean.workbook}"
                     validator="#{excelImporterBean.validate}"
                     style="display: none">
            <f:ajax execute="@this" render="excelMessage :tblTravels"
                    listener="#{excelImporterBean.onLoadWorkbook}"/>
        </h:inputFile>
        <h:message id="excelMessage" for="excel" errorClass="error-message"/>
        <h:outputLink value="#{request.contextPath}/home/recorridos/nuevo"
                      styleClass="ui primary button">
            Registrar
        </h:outputLink>
    </div>
</h:form>

This are the ExcelImporterBean methods (validator and listener):

public void onLoadWorkbook(AjaxBehaviourEvent ev) {
        excelImporter.importWorkbook(workbook);
    }

    public void validate(FacesContext ctx, UIComponent comp, Object value) {
        FacesMessage message = null;
        Part _workbook = (Part) value;

        if(((Part) value).getContentType().equals(EXCEL_TYPE)) {
            message = new FacesMessage("Solo archivos excel 2007+ (.xlsx)");
        }
        if(message != null) {
            throw new ValidatorException(message);
        }
    }

The app is running on:

  • Tomcat 8.0.35
  • JSF (mojarra) 2.2.13

Edit

  • On Firefox the error is: partialResponse is not defined
  • On Chrome is: cannot read property getAttribute of undefined
  • 1
    Did you experiment with several various JSF impls/versions in a short time? Try cleaning your browser cache or testing in an incognito window. Chances are that you've still the jsf.js script of an older or different JSF implementation in browser cache. – BalusC Jun 05 '16 at 05:48
  • Hi Bauke, thanks for answer. The issue is strange, I have test a simple form ajax upload with Mojarra 2.2.1 (default of IntelliJ) and everything works. I have put the same Mojarra version into the POM and the result is the same. That's mean I have a logic error maybe? With Mojarra 2.2.1 a new JS error is thrown: Cannot read property length of null. I don't understand nothing. –  Jun 05 '16 at 12:54
  • Fixed. The problem is due to Rewrite (before PrettyFaces). I wrote the solution. Thanks. –  Jun 05 '16 at 13:15

1 Answers1

1

The problem is due to Rewrite(before PrettyFaces). For fix this you need to add the attribute allowCasualMultipartParsing="true" in your context.xml file inside META-INF. Example:

<Context path="/MyApp" allowCasualMultipartParsing="true">
  <Resource auth="Container" 
            factory="org.jboss.weld.resources.ManagerObjectFactory" 
            name="BeanManager" 
            type="javax.enterprise.inject.spi.BeanManager"/>
</Context>