Hi,

 

I’m having a problem with <t:saveState>. I want to use it to pass a whole bean between one view and another but when I get to the second view the bean is empty. This appears like it should be easy and I’ve followed the previous threads on this issue and I think I’m doing everything correctly, but obviously not.

 

Any help would be greatly appreciated.

 

Thanks in advance.

 

Tom.

 

[FIRST JSP]:

 

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>

 

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"

  xmlns:f="http://java.sun.com/jsf/core"

  xmlns:h="http://java.sun.com/jsf/html"

  xmlns:htm="http://jsftutorials.net/htmLib"

  xmlns:t="http://myfaces.apache.org/tomahawk">

  <jsp:directive.page contentType="text/html; charset=iso-8859-1"/>

 

  <html>

    <body>

      <f:view>

        <t:saveState id ="fileUploadBean" value="#{fileUploadBean}" />

        <h:form

          id = "FileUploadExampleForm"

          enctype = "multipart/form-data">

          <h:panelGrid columns = "3" border="0" cellspacing="5">

            <h:outputLabel for="" value="File: "/>

            <t:inputFileUpload

              id="myFileId"

              value="#{fileUploadBean.uploadedFile}"

              storage="file"

              required="true"/>

            <h:message for="">

 

            <h:outputText value=" "/>

            <h:commandButton

              value="Upload"

              action="">

            <h:outputText value=" "/>

          </h:panelGrid>

        </h:form>

      </f:view>

    </body>

  </html>

</jsp:root>

 

[SECOND JSP]:

 

<?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>

 

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2"

  xmlns:f="http://java.sun.com/jsf/core"

  xmlns:h="http://java.sun.com/jsf/html"

  xmlns:htm="http://jsftutorials.net/htmLib"

  xmlns:t="http://myfaces.apache.org/tomahawk">

  <jsp:directive.page contentType="text/html; charset=iso-8859-1"/>

 

  <html>

    <body>

      <f:view>

        <h:panelGrid columns="2" border="0" cellspacing="5">

          <h:outputText value="File Name:"/>

          <h:outputText value="#{fileUploadBean.uploadedFile.name}"/>

          <htm:br/>

          <h:outputText value="File Size:"/>

          <h:outputText value="#{fileUploadBean.uploadedFile.size}"/>

          <htm:br/>

          <h:outputText value="Contents:"/>

          <h:outputText value="#{fileUploadBean.fileContents}"/>

        </h:panelGrid>

      </f:view>

    </body>

  </html>

</jsp:root>

 

[BEAN TO BE SAVED]:

 

package com.anabus.myfacesexamples.myfacesfileupload;

 

import org.apache.myfaces.custom.fileupload.UploadedFile;

 

import javax.faces.application.FacesMessage;

import javax.faces.context.FacesContext;

 

import java.io.*;

import org.apache.log4j.Logger;

 

import com.anabus.myfacesexamples.*;

 

public class FileUploadBean implements Serializable {

 

  protected static Logger logger = Logger.getLogger("TAH");

 

  private UploadedFile uploadedFile;

  private StringBuffer fileContents;

 

  public UploadedFile getUploadedFile() {

    return uploadedFile;

  }

 

  public void setUploadedFile(UploadedFile uploadedFile) {

    this.uploadedFile = uploadedFile;

  }

 

  public String getFileContents() {

    String returnString = "No File Content.";

 

    if ((fileContents != null) &&

        (fileContents.length()>0)) {

      returnString = fileContents.toString();

    }

 

    return returnString;

  }

 

  public String gotoUploadedFileOutput() {

    String destination = "";

 

    destination = NAV.UPLOAD_FILE_OUTPUT;

 

    return destination;

  }

 

  public String processFile() {

    InputStream inputStream;

 

    logger.info("FileUploadBean.processMyFile() START");

    try {

      inputStream = new BufferedInputStream(uploadedFile.getInputStream());

      fileContents = new StringBuffer();

      int ch=0;

      while((ch = inputStream.read()) > -1) {

        fileContents.append((char)ch);

      }

      fileContents.append(inputStream.toString());

      inputStream.close();

      return gotoUploadedFileOutput();

    }

    catch (Exception x) {

      logger.info("FileUploadBean.processMyFile() Error: " + x.getClass().getName());

      FacesMessage message = new FacesMessage(

          FacesMessage.SEVERITY_FATAL,

          x.getClass().getName(), x.getMessage());

      FacesContext.getCurrentInstance().addMessage(

          null, message);

      return null;

    }

  }

}

 

[FACES-CONFIG.XML]

 

  <!-- File Upload Example -->

  <managed-bean>

    <managed-bean-name>fileUploadBean</managed-bean-name>

    <managed-bean-class>com.anabus.myfacesexamples.myfacesfileupload.FileUploadBean</managed-bean-class>

    <managed-bean-scope>request</managed-bean-scope>

  </managed-bean>

 

[WEB.XML]

 

  <context-param>

    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>

    <param-value>client</param-value>

  </context-param>

 

Reply via email to