图片属性未更新。我使用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)这是我的xhml页面。
<!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>
答案 0 :(得分:1)
您的问题是由这两个原因的组合引起的:
<p:fileUpload>
正在“高级模式”运行(使用浏览,上传和取消按钮)。请求范围的bean的生命周期恰好是一个HTTP请求。使用<p:fileUpload>
按钮帐户上传文件作为一个HTTP请求。提交表单帐户作为另一个HTTP请求,具有自己的全新bean实例。在user.getImage()
执行简单调试时,您应该注意到em.persist()
为空/空。
为了使其工作,您需要将bean放在视图范围内。这样,只要您与同一视图交互,bean就会存在。
@ManagedBean
@ViewScoped
public class UserUpdateBean {}
(或使用旧式XML配置方法时使用<managed-bean-scope>view
)
另一种方法是将<p:fileUpload>
设置为“简单模式”,这样它只带有一个浏览按钮,只有在您实际提交表单时才会上传。这样,上传的文件将在相同的 HTTP请求期间设置,就像提交表单时一样。
<p:fileUpload ... mode="simple" />