图像属性未更新。我正在使用 this.user.setImage(a) 来设置值,但它不起作用。我的托管 bean 是请求范围的。
1)这是我的bean。问题出在handleFileUpload函数中。
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)这是我的 xml 页面。
<!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>