3

我正在使用带有 primfaces 的 jsf2.0 并使用 p:fileupload 来上传照片。这里我需要在支持 bean 中传递参数。因为没有通过 p:fileupload 传递参数的选项。我也使用了绑定选项,但它在支持 bean 上返回空值...

这是我的 fileupload.xhtml

   <h:form  enctype="multipart/form-data">
            <p:panel  header="Upload Photos" id="getImageId" 
             style="font-size:12px;height:499px">
            <p:messages id="messages" for="imaload"></p:messages>
                 <p:fileUpload id="imaload" fileUploadListener="#  
       {ngoPhotoUpload.photoUpload}"  
                           mode="advanced"  multiple="true"   
     immediate="true"
                          update="messages" 
                           allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>

                 <!--  <p:growl life="1000" id="messages"/>   -->

            </p:panel>
        </h:form>

这是我的支持 bean 方法

public void photoUpload(FileUploadEvent event) throws IOException,
        InterruptedException {

    BufferedImage bufferedImage;
    String tmpFile = scontext.getRealPath(("/ngoPhotos/")
            + event.getFile().getFileName());
    File result = new File(tmpFile);

    byte[] imageByte = event.getFile().getContents();

    storeImage(imageByte, tmpFile);

    String imageName = event.getFile().getFileName();
    String createdby = loginBean.getEmail();

    if (loginBean.getType().equals("admin")
            || loginBean.getType().equals("ngo_coordinator")) {
        if (ngoRegnPojo.getNgo_id() != 0) {
            ngoPhotoBean.setNgo_id(ngoRegnPojo.getNgo_id());
        } else {
            ngoPhotoBean.setNgo_id(loginBean.getUser_id());
        }
    } else {
        ngoPhotoBean.setNgo_id(loginBean.getUser_id());
    }

    ngoPhotoBean.setImageName(imageName);
    ngoPhotoBean.status = "status";
    ngoPhotoBean.setCreatedDate(new Date());
    ngoPhotoBean.setCreatedby(createdby);
    //ngoPhotoBean.setPathName(tmpFile);
    ngoPhotoBean.disable = "false";
    bufferedImage = ImageIO.read(result);
    if (bufferedImage.getWidth() <= 400 && bufferedImage.getHeight() <= 400) {

        ngoPhotoBean.disable = "false";
        getMthd(imageByte, tmpFile);

    } else {

        try {

            ngoPhotoBean.disable = "false";
            bufferedImage = ImageIO.read(result);
            ImageIO.write(resize(bufferedImage, 400, 400), "jpg", new File(
                    tmpFile));
            photoUploadDaoService.uploadNgoPhoto(ngoPhotoBean);
            NgoPhotoBean ngoPhotoBean = new NgoPhotoBean();
            FacesContext.getCurrentInstance().getExternalContext()
                    .getSessionMap().put("ngoPhotoBean", ngoPhotoBean);
            FacesMessage msg = new FacesMessage("Successfully Uploaded");

            FacesContext.getCurrentInstance().addMessage(null, msg);

        } catch (Exception e) {
            e.printStackTrace();
            FacesMessage error = new FacesMessage(
                    FacesMessage.SEVERITY_ERROR,
                    "The files were not uploaded!", "");
            FacesContext.getCurrentInstance().addMessage(null, error);
        }
    }
}

提前致谢

4

2 回答 2

1

您可以使用p:remotecommand,例如使用文件上传的 oncomplete 可以调用 p:remote 命令并调用传递参数的函数。我在我的项目中做了类似的事情,效果很好。

于 2012-06-07T07:25:20.470 回答
1

您需要传递什么样的参数?如果它只是某种常量,您可以在表单中放置一个隐藏字段,以便在提交后接收它的值。

于 2012-06-06T07:02:48.657 回答