图片未更新 [英] Image not getting updated

查看:106
本文介绍了图片未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

image属性未更新.我正在使用this.user.setImage(a)来设置值,但是它不起作用.我的托管bean是请求范围的.

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)这是我的bean.问题出在handleFileUpload函数中.

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)这是我的xhml页面.

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>

推荐答案

您的问题是由以下两种原因共同引起的:

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

  1. 该bean是请求范围的.
  2. <p:fileUpload>以高级模式"运行(带有浏览,上传和取消按钮).
  1. The bean is request scoped.
  2. The <p:fileUpload> is running in "advanced mode" (with browse, upload and cancel buttons).

一个请求范围的Bean的生存期仅为一个HTTP请求.使用<p:fileUpload>按钮上载文件作为一个HTTP请求.将表单帐户作为另一个HTTP请求提交,并带有其自己的Bean全新实例.您应该注意到user.getImage()在进行简单调试时em.persist()期间为空/空.

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.

为了使其工作,您需要将bean放在视图作用域中.这样,只要您与同一个视图进行交互,bean就会一直存在.

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 {}

(或在使用老式XML配置方法时使用<managed-bean-scope>view)

另一种方法是将<p:fileUpload>设置为简单模式",以便它仅带有浏览按钮,并且仅在您实际提交表单时才上载.这样,上传的文件将在与提交表单时的相同 HTTP请求期间进行设置.

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" />

另请参见:

  • 如何选择正确的bean作用域?
  • See also:

    • How to choose the right bean scope?
    • 这篇关于图片未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆