title: Injecting a SessionScoped Bean inside a View Scoped bean in JSF
body: <p>I am building a web application in JSF 2.0 using Prime faces 4.0.In my project I have image upload feature for this I have used  of Prime Faces. My requirement is this:there is a Choose button through which user will browse and then through Upload button
  image will be uploaded,but it will not be saved in database.There is a separate save button in the page on clicking the save button image will get saved in database.</p>
  
  <p>But my problem is that::For uploading image I have a managedBean calle ImageController.java which is in SessionScoped and the save button consist of a actionlistener method which is defined inside a managedBean class which is in ViewScoped.</p>
  
  <p>I am providing the code of ImageController.java class</p>
  
  <pre><code>   package com.edfx.tum.web.controller;
  
     import java.io.ByteArrayInputStream;
     import java.io.FileNotFoundException;
     import java.io.IOException;
     import java.io.Serializable;
     import javax.annotation.PostConstruct;
     import javax.faces.bean.ManagedBean;
     import javax.faces.bean.SessionScoped;
     import org.primefaces.event.FileUploadEvent;
     import org.primefaces.model.DefaultStreamedContent;
     import org.primefaces.model.StreamedContent;
     import org.primefaces.model.UploadedFile;
     import com.edfx.tum.dto.UserDTO;
  
  
    @SessionScoped
    @ManagedBean(name = "imageController")
    public class ImageController extends BaseWebController implements Serializable {
  
   private static final long serialVersionUID = -3975946403680318499L;
  
   private StreamedContent image;
   private byte[] uploadedImage;
  
  
  
  public void initImage() {
  
      UserDTO user = getCurrentUser();
      byte[] img = user.getUserPhoto();
      setUploadedImage(img);
      if (img != null) {
          image = new DefaultStreamedContent(new ByteArrayInputStream(img));
      } else {
          image = null;
      }
  }
  
  public StreamedContent getImage() {
      return this.image;
  }
  
  
  
  public byte[] getUploadedImage() {
      return uploadedImage;
  }
  
  public void setUploadedImage(byte[] uploadedImage) {
      this.uploadedImage = uploadedImage;
  }
  
  
  public void setImage(StreamedContent image) {
      this.image = image;
  }
  
  
  @PostConstruct
  public void init() throws FileNotFoundException, IOException {
      initImage();
  }
  
      public void handleFileUpload(FileUploadEvent event) {
      UploadedFile uploadedFile = event.getFile();
  
      byte[] img = uploadedFile.getContents();
  
      image = new DefaultStreamedContent(new ByteArrayInputStream(img));
      setUploadedImage(img);
   }
  }
  </code></pre>
  
  <p>The name of the managedBean to which save button's actionlistener is bind is UserDetailsController and in tghe following way I have injected the dependency into UserDetailscontroller.java class</p>
  
  <pre><code>@ViewScoped
  @ManagedBean(name = "userDetailsController")
  public class UserDetailsController extends BaseWebController implements Serializable {
  @ManagedProperty(value = "#{imageController}")
  private ImageController imageController;
  
  public ImageController getImageController() {
      return imageController;
  }
  
  public void setImageController(ImageController imageController) {
      this.imageController = imageController;
   }
  
    //other codes here
  
    public void submit(ActionEvent actionEvent) {
      try {
          UserDTO userDTO = (UserDTO) actionEvent.getComponent().getAttributes().get("user");
          if (userDTO != null) {
              userDTO.setUserPhoto(imageController.getUploadedImage());
              processDBPeopleUpdate(userDTO);
          }
      } catch (Exception e) {
          LoggingUtils.printStackTrace(e, log);
      }
    }  
  }
  </code></pre>
  
  <p>donot worry about the "processDBPeopleUpdate(userDTO);" code it just normal saveOrUpdate() method for saving the dto to database.</p>
  
  <p>below is the piece of xhtml code::</p>
  
  <pre><code>  &lt;p:fileUpload id="fileUpload" styleClass="imageStyle" mode="advanced" dragDropSupport="false" update="myProfileForm:growl myProfileForm:profileImage"     sizeLimit="1000000" fileUploadListener="#{imageController.handleFileUpload}" allowTypes="/(\.|\/)(gif|jpeg|png|tiff|exif)$/"/&gt;
  </code></pre>
  
  <p>Now,the problem I am facing is that after clicking the save button picture is getting saved in database.but once I changed the tab for example:<strong>This functionality is in "My Profile" tab now after saving the image in database if I change the tap to User(suppose),from again I goto My Profile tab the picture is not showing</strong></p>
  
  <p>again If I logout then do login and go to "my profile tab" again the picture is  showing.So,In a word I want to say that image is not persisting upon tab change.</p>
  
  <p>can anyone give any suitable solution to this?</p>