3 Replies Latest reply on Jun 26, 2008 4:09 AM by kosmos

    inputText not Updating in Backing Bean

    amattas

      I'm trying to upload a file and pull data in from a description field when the listener processes the request. Here is the applicable code, however the description always ends up being null in the database.

       <h:form enctype="multipart/form-data" id="upload">
       <h:panelGrid columns="2">
       <h:outputText value="Description: " id="description" />
       <h:inputText value="#{OrderView.uploadDescription}" id="uploadDescription" immediate="true" >
       <a4j:support event="onkeyup" reRender="upload" />
       </h:inputText>
       </h:panelGrid>
       <rich:fileUpload id="upload"
       immediateUpload="true" fileUploadListener="#{OrderView.addUpload}"
       disabled="#{fn:length(OrderView.uploadDescription) lt 10}">
       </rich:fileUpload>
       <span id="submitbutton"><h:commandButton value="Done"
       action="success" styleClass="button" /> </span>
       </h:form>
      


       public void addUpload(UploadEvent event) {
       if (event == null) { _logger.warning("null upload event"); }
       UploadItem uploadItem = event.getUploadItem();
       FileInputStream inputStream;
       FileOutputStream outputStream;
       FacesContext facesContext = FacesContext.getCurrentInstance();
       ExternalContext externalContext = facesContext.getExternalContext();
       String username = externalContext.getUserPrincipal().getName();
       String filename = Long.toString(new Date().getTime());
       String extension = fileExtension(uploadItem.getFileName());
       String uploadLocation = this.getUploadParent() + '/' + this.getUploadSuffix() + '/';
       File outputDir = new File(uploadLocation);
       if (!outputDir.exists()) outputDir.mkdirs();
       try {
       System.out.println(uploadLocation + filename + extension);
       outputStream = new FileOutputStream(uploadLocation + filename + extension);
       if (uploadItem.isFile()) {
       inputStream = new FileInputStream(uploadItem.getFile());
       outputStream.getChannel().transferFrom(inputStream.getChannel(), 0, inputStream.getChannel().size());
       inputStream.close();
       } else
       outputStream.write(uploadItem.getData());
       outputStream.close();
       setters.addFile(this.getId(), filename+extension, this.getUploadDescription(), username);
       } catch (FileNotFoundException e) {
       _logger.severe("input file not found");
       } catch (IOException e) {
       _logger.severe("can't write output file");
       } catch (UnknownUserException e) {
       _logger.severe("upload user does not exist");
       } catch (UnknownOrderException e) {
       _logger.info("upload order does not exist");
       }
      
      
       }
      


        • 1. Re: inputText not Updating in Backing Bean

          Hi,

          I tried your code. It works fine for me using 3.2.1 RF.
          Description is NOT null in uploadListener. 'OrderView' bean is session scoped in my case.

          Anyway, I cannot understand why do you force AJAX request after every 'onkeyup' event.

          There is more convenient way to enable fileUpload component after 10 character were entered in description field: please use JS API.

          It should be like this:

          <h:inputText id='descriptionID' onkeyup='return enableFileUpload(this)' .../>
          
          <script>
          function enableFileUpload (elt) {
           if (elt.value && elt.value.length >= 10) {
           $('fileUploadID').component.enable();
           }
          return true;
          }
          </script>


          Please see documentation for more detailed info about JS API.

          • 2. Re: inputText not Updating in Backing Bean
            amattas

            Is it possible to do this in a request scoped bean?

            I've run into an issue using a4j:keepAlive that breaks it because of some objects needed for my persistance layer to function properly aren't serializable, so I don't think I can do it this way.

            • 3. Re: inputText not Updating in Backing Bean
              kosmos