primeFaces无法将文件上传到数据库 [英] primeFaces fails to upload file to db

查看:170
本文介绍了primeFaces无法将文件上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将作为Product对象一部分的文件(图片)上传到数据库.根据jsf确认,文件上传成功.但是,当持久化Product时,持久性将成功完成而没有图片.服务器未返回任何堆栈.

I am trying to upload a file (picture) which is part of a Product object to a database. The file upload is successful according to the jsf confirmation. However, when persisting Product the persistence is completed successfully without the picture. The server is not returning any stack.

这是我的表格(简体):

This is my form (simplified):

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

此产品控制器(简体):

This the product Controller (Simplified):

@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();        
    }

这是产品实体(简体):

This is the product entity(Simplified):

@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的范围更改为:

SOLUTION: I changed the scope of the managed bean to:

@ViewScope

推荐答案

文件上传发生在第一个请求中.

Fileupload happens in a first request.

当表单与输入数据一起提交时,将启动一个新的第二个请求.将创建一个新的requestScoped ManagedBean,它不知道先前的文件上传.

When form is submited with the input data, a new 2nd request will be initiated. A new requestScoped ManagedBean will be created, that doesnt know about the previous fileupload.

要允许两个请求共享相同的Managedbean,请将范围更改为:

To allow the two requests to share the same Managedbean, Change the scope to:

@ViewScoped 

这篇关于primeFaces无法将文件上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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