我正在尝试将作为Product
对象一部分的文件(图片)上传到数据库。根据jsf确认文件上传成功。但是,在Product
没有图片的情况下,持久化成功完成。服务器没有返回任何堆栈。
这是我的表格(简化):
<h:form enctype="multipart/form-data">
<h1><h:outputText value="Create New Product"/></h1>
<h:panelGrid columns="2">
<h:outputLabel value="Name:" for="name" />
<h:inputText id="name" value="#{productController.product.name}" title="Name" />
<h:outputLabel value="Description:" for="description" />
<h:inputText id="description" value="#{productController.product.description}" title="Description" />
<h:outputLabel value="Price:" for="price" />
<h:inputText id="price" value="#{productController.product.price}" title="Price" />
<h:outputLabel value="Category:" for="category_fk" />
<h:selectOneMenu id="category_fk" value="#{productController.product.category_fk}"
converter="#{categoryConverter}" title="Category_fk" >
<f:selectItems value="#{productController.categoryList}" var="prodCat"
itemValue="#{prodCat}" itemLabel="#{prodCat.name}"/>
</h:selectOneMenu>
<p:fileUpload fileUploadListener="#{productController.handleFileUpload}" update="msg" auto="true" />
<p:growl id="msg" showDetail="true"/>
<h:inputHidden id="dateAdded" value="#{productController.product.dateAdded}">
<f:convertDateTime pattern="yyyy/MM/dd HH:mm:ss" />
</h:inputHidden>
</h:panelGrid>
<h:commandButton value="Create Product" action="#{productController.doCreateProduct()}"/>
</h:form>
本产品控制器(简体):
@ManagedBean
@RequestScoped
public class ProductController {
@EJB
private ProductEJB productEjb;
@EJB
private CategoryEJB categoryEjb;
private Product product = new Product();
private List<Product> productList = new ArrayList<Product>();
private Category category;
private List<Category> categoryList = new ArrayList<Category>();
// -------------------------------------------------------- Business Methods
public String doCreateProduct()
{
product = productEjb.createProduct(product);
productList = productEjb.findAllProducts();
return "listProduct?faces-redirect=true";
}
public String doDeleteProduct()
{
productEjb.deleteProduct(product);
return "listProduct?faces-redirect=true";
}
public String cancelDeleteAction()
{
return "listProduct?faces-redirect=true";
}
// Update product listing
public void preRenderView()
{
if(product == null)
{
product = new Product();
}
}
public String doUpdateProduct()
{
if(product.getProduct_id() != 0)
{
productEjb.updateProduct(product);
}
else
{
productEjb.createProduct(product);
}
//addFlashMessage("Product " + product.getName() + " (" + product.getProduct_id() + ") has been updated");
return "listProduct?faces-redirect=true";
}
public void handleFileUpload(FileUploadEvent event)
{
byte[] uploadedFile = new byte[event.getFile().getContents().length];
System.arraycopy(event.getFile().getContents(), 0, uploadedFile, 0, event.getFile().getContents().length);
product.setImageFile(uploadedFile);
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + "is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
@PostConstruct
public void init()
{
categoryList = categoryEjb.findAllCategory();
productList = productEjb.findAllProducts();
}
这是产品实体(简体):
@Entity
@NamedQueries({
@NamedQuery(name="findAllProducts", query = "SELECT p from Product p")
})
public class Product implements Serializable
{
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy= GenerationType.AUTO)
private int product_id;
private String name;
private String description;
@Lob
@Column(name="imageFile")
protected byte[] imageFile;
private Float price;
@Temporal(TemporalType.TIMESTAMP)
private Date dateAdded;
@ManyToOne
private Category category_fk;
@OneToOne(mappedBy = "product_fk")
private SaleDetails saleDetails_fk;
解决方案: 我将托管 bean 的范围更改为:
@ViewScope