Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8489789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:55:33+00:00 2026-06-10T21:55:33+00:00

I have a view scoped bean where I create a person. A person can

  • 0

I have a view scoped bean where I create a person. A person can have a picture. This picture is uploaded the same page the person is created. The picture is not stored in a database or on disk (since the person isn’t created yet). The bean has to be view scoped since a person can be created elsewhere and this uses the same bean. If the bean is session scoped and a user uploads a picture but does not save the person, the picture will be displayed next time the user tries to create a person.

I solved this by using two beans; one view scoped bean to create the person and a session scoped bean to upload the picture and to get the picture as a stream. This however causes the problem noted above.

How can I solve this in a better way?

The upload bean:

@ManagedBean(name = "uploadBean")
@SessionScoped
public class UploadBean
{
    private UploadedFile    uploadedFile;

    public UploadedFile getUploadedFile()
    {
        return uploadedFile;
    }

    public StreamedContent getUploadedFileAsStream()
    {
        if (uploadedFile != null)
        {
            return new DefaultStreamedContent(new ByteArrayInputStream(uploadedFile.getContents()));
        }
        return null;
    }

    public void uploadFile(FileUploadEvent event)
    {
        uploadedFile = event.getFile();
    }
}

The create-a-person bean:

@ManagedBean(name = "personBean")
@ViewScoped
public class PersonBean
{
    private Person newPerson = new Person();

    public Person getNewPerson()
    {
        return newPerson;
    }

    private UploadedFile getUploadedPicture()
    {
        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();
        UploadBean uploadBean = (UploadBean) elContext.getELResolver().getValue(elContext, null, "uploadBean");
        return uploadBean.getUploadedFile();
    }

    public void createPerson()
    {
        UploadedFile uploadedPicture = getUploadedPicture();
        // Create person with picture;
    }
}

The relevant JSF page part:

<h:form enctype="multipart/form-data">
    <p:outputPanel layout="block" id="personPicture">
        <p:graphicImage height="150"
            value="#{uploadBean.uploadedFileAsStream}"
            rendered="#{uploadBean.uploadedFileAsStream != null}" />
    </p:outputPanel>
        <p:fileUpload auto="true" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
            fileUploadListener="#{uploadBean.uploadedFile}"
            update="personPicture" />
    <p:commandButton value="Save" actionListener="#{personBean.createPerson()}"/>
</h:form>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:55:34+00:00Added an answer on June 10, 2026 at 9:55 pm

    I’ve gone for a different approach. I initially went for displaying an uploaded image, however if the Person isn’t created yet it seemed like a better idea to keep it all client side. I found this question and created the following based on the chosen answer:

    In the head I include html5shiv if the browser is IE and the version is less than 9 for compatibility:

    <h:outputText value="&lt;!--[if lt IE 9]&gt;" escape="false" />
    <h:outputScript library="js" name="html5shiv.js" />
    <h:outputText value="&lt;![endif]--&gt;" escape="false" />
    

    To display/upload the image I have these elements:

    <p:fileUpload binding="#{upload}" mode="simple"
        allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
        value="#{personBean.uploadedPicture}"/>
    <p:graphicImage value="#" height="150" binding="#{image}" />
    

    And some JavaScript/jQuery magic:

    function readPicture(input, output)
    {
        if (input.files && input.files[0])
        {
            var reader = new FileReader();
            reader.onload = function(e)
            {
                output.attr('src', e.target.result);
            };
            reader.readAsDataURL(input.files[0]);
        }
    }
    
    $("[id='#{upload.clientId}']").change(
        function()
        {
            readPicture(this, $("[id='#{image.clientId}']"));
        });
    

    The uploadedPicture property is now a simple property:

    @ManagedBean(name = "personBean")
    @ViewScoped
    public class PersonBean
    {
        private UploadedFile uploadedPicture;
    
        public UploadedFile getUploadedPicture()
        {
            return uploadedPicture;
        }
    
        public void setUploadedPicture(UploadedFile uploadedPicture)
        {
            this.uploadedPicture = uploadedPicture;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I access the view parameters from a viewscoped bean? I have a
I have a view-scoped bean that implements Serializable , and a UIComponent passed in
I have the view scoped bean which should access on init (@PostConstruct) the values
I have a view-scoped bean ManageFoo.java: @ManagedBean(name = ManageFoo) @ViewScoped public class ManageFoo {
I have a method in a view Scoped Bean with the @PreDestroy annotation and
I have a page that is backed by a ViewScoped Managed Bean. The page
i have view like 'home/details/5', it can be access by anonymous user. but there
I have view page with both detailview and gridview the grid view should be
I have the following conversation scoped backing bean: @Named @ConversationScoped public class TestConversation implements
I have a JPA entity Person as person can have many contacts (these are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.