1

In my application, I have the following form

<h:form id="addItemForm" enctype="multipart/form-data">                      
    <h:panelGrid columns="3" border="1">
        <h:outputText value="Name: " />
        <h:inputText label="name" 
                     id="name" value="#{addItem.name}" 
                     required="true" requiredMessage="Name is required." />
        <h:message styleClass="errorMsg" for="name" />

        <h:outputText value="Description: " />
        <h:inputText label="description" 
                     id="description" value="#{addItem.description}" 
                     required="true" requiredMessage="Description is required." />
        <h:message styleClass="errorMsg" for="description" />

        <h:outputText style="font-weight: bold" value="Picture : " />
        <t:inputFileUpload label="picture" 
                           id="picture" value="#{addItem.picture}" 
                           required="true" requiredMessage="Picture is required." />
        <h:message styleClass="errorMsg" for="picture" />

    </h:panelGrid>
    <h:commandButton value="Confirm" actionListener="#{addItem.addItem}"/>
    <h:outputText id="status" value="#{addItem.statusMsg}" />
</h:form>

If I enter the description & leave the name as blank and I click the Confirm button, I don't see any error message for the missing name. One weird thing is that my page seems to be refreshed because anything that I typed in for description was gone. Besides, even if I typed in both name and description, the addItem function in the actionListener was never called.

Yesterday, everything was working. I'd be very grateful if someone could tell me what I might have done wrong here.

Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90
  • You have to use ajax (` – Bhesh Gurung Oct 17 '11 at 19:04
  • By "refresh" I mean after I click the Confirm button, it seems like a whole new request which is not related to the previous request at all. My ManagedBean is in ViewScope which means the description that I typed should be here even if I didn't type the name. – Mr.J4mes Oct 17 '11 at 19:13
  • But if the validation fails then the `Update Model Values` phase will be escaped right? – Bhesh Gurung Oct 17 '11 at 19:25
  • Actually it is not, the "Apply request values" phase happens before the "Validation" phase. Hence, whatever I typed in should be available even if I failed the Validation phase. – Mr.J4mes Oct 17 '11 at 19:47

1 Answers1

2

Remove enctype="multipart/form-data". JSF doesn't have native support for it. JSF relies the submitted request parameters being available as application/x-www-form-urlencoded instead, which is already the default form encoding. You also don't have any <input type="file"> fields in your form (neither plain, nor represented by a 3rd party JSF component), so you don't need the multipart/form-data encoding at all.

Perhaps it worked before because you had a filter provided by some 3rd party component library which parses and prepares multipart/form-data requests for JSF, but you have removed it from your web.xml, or you have not included it in your new project.


Update: you edited the question to include <t:inputFileUpload>. Okay, you should keep the enctype="multipart/form-data". The only reason that the form isn't submitted at all can be that the ExtensionsFilter has been removed from web.xml or failed to initialize. Add it and/or read the webapp startup logs. It's the one who's responsible for parsing multipart/form-data requests into useable parameters for JSF. Another possible reason is that you nested multiple <h:form> components into each other which would generate invalid HTML. See also commandButton/commandLink/ajax action/listener method not invoked or input value not updated.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Actually, I do have a file input field, I tried to shorten my code so people don't have to read too much. Thanks a lot for your help! You're really so experienced at JavaEE. When I am merging my code with my friend's code from the repository, I forgot to update the web.xml. Hence, I lost my filter and filter mapping settings. – Mr.J4mes Oct 17 '11 at 19:45