Wizard Sultan
Wizard Sultan

Reputation: 918

Image not getting updated

The image property is not getting updated. I am using this.user.setImage(a) to set the value but its not working.My managed bean is request scoped.

1)Here is my bean.The problem is in the handleFileUpload function.

package com.bean;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpSession;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.entity.User;

public class UserUpdateBean {
    User user;

    private EntityManager em;
    private UploadedFile uploadedFile;
    public UserUpdateBean(){
        EntityManagerFactory emf=Persistence.createEntityManagerFactory("FreeBird");
        em=emf.createEntityManager();
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        User s=(User) session.getAttribute("userdet");
        user = em.find(User.class,s.getEmail());
    }

    public void handleFileUpload(FileUploadEvent e) {  
        System.out.println("file handler called");
        uploadedFile = e.getFile();
        String fileName=uploadedFile.getFileName();
        fileName=fileName.substring(fileName.lastIndexOf("\\")+1);
        System.out.println(fileName);
        byte[] a = uploadedFile.getContents();
        PhaseId currentPhaseId = FacesContext.getCurrentInstance().getCurrentPhaseId();
        System.out.println("current phase id"+currentPhaseId);
        user.setImage(a);

    }   
public String update(){

    em.getTransaction().begin();
    em.persist(user);
    em.getTransaction().commit();
    System.out.println("updated successful");
    FacesContext context = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
    session.setAttribute("userdet", user);
    return "success";


}
public User getUser() {
    return user;
}
public void setUser(User user) {
    this.user = user;
}


}

2)Here is my xhml page.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head></h:head>

    <h1> Main Content </h1>
    <h:form enctype="multipart/form-data">
    <h:panelGrid columns="2">
                    <h:outputText value="Profile Image"/>
                    <p:fileUpload fileUploadListener="#{userUpdateBean.handleFileUpload}"/>
                    <h:outputText value="Username: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.userName}" />
                    <h:outputText value="Firstname: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.firstName}" />
                    <h:outputText value="Lastname: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.lastName}" />
                    <h:outputText value="Password: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.password}" />
                    <h:outputText value="Date of Birth: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.birthDate}" >
                                <f:convertDateTime pattern="dd/MM/yyyy"></f:convertDateTime>
                                </p:inputText>
                    <h:outputText value="Gender: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.gender}" />
                    <h:outputText value="Relationship: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.relationship}" />  
                    <h:outputText value="EmailID: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.email}" /> 
                    <h:outputText value="Contact No.: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.contactNo}" />
                    <h:outputText value="Street: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.street}" />
                    <h:outputText value="City: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.city}" />  
                    <h:outputText value="Pincode: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.pincode}" />   
                    <h:outputText value="State: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.state}" />
                    <h:outputText value="Country: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.country}" />       
                    <h:outputText value="Secondary School: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.secondarySchool}" />
                    <h:outputText value="High School: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.highSchool}" />
                    <h:outputText value="College: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.college}" />   
                    <h:outputText value="University: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.university}" />
                    <h:outputText value="Degree: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.degree}" />
                    <h:outputText value="Quote: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.quote}" />
                    <h:outputText value="About Me: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.aboutMe}" />   
                    <h:outputText value="Interest: " />
                    <p:inputText styleClass="outputLabel"
                                value="#{userUpdateBean.user.interest}" />                                                                                                                                                                                                                                                      
                    <h:outputText />
                    <h:outputText />
                    <h:commandButton value="Update" action="#{userUpdateBean.update}"/>
                    </h:panelGrid>
                    </h:form>


</h:body>
</html>

Upvotes: 1

Views: 299

Answers (1)

BalusC
BalusC

Reputation: 1108642

Your problem is caused by a combination of those 2 causes:

  1. The bean is request scoped.
  2. The <p:fileUpload> is running in "advanced mode" (with browse, upload and cancel buttons).

A request scoped bean has a lifetime of exactly one HTTP request. Uploading a file using <p:fileUpload> button accounts as one HTTP request. Submitting the form accounts as another HTTP request, with its own brand new instance of the bean. You should have noticed it by user.getImage() being null/empty during em.persist() while doing a simple debug.

In order to get it to work, you need to put the bean in the view scope. This way the bean will live as long as you're interacting with the same view.

@ManagedBean
@ViewScoped
public class UserUpdateBean {}

(or with <managed-bean-scope>view when using the old fashioned XML config approach)

An alternative is to set <p:fileUpload> to "simple mode" so that it comes with only a browse button and get uploaded only when you actually submit the form. This way the uploaded file will be set during the same HTTP request as when the form is submitted.

<p:fileUpload ... mode="simple" />

See also:

Upvotes: 1

Related Questions