导航以调用bean类的操作

时间:2011-11-17 14:58:30

标签: jsf jsf-2 primefaces

我正在使用JSF 2.0和PrimeFaces 3.0。我上传了图片并且必须裁剪图像。图像上传并成功显示在上传页面中。

当我选择图像并单击裁剪按钮时,不会调用相应的裁剪bean。如果我没有选择图像并单击裁剪按钮,则会调用相应的裁剪bean类,但发生了NullPointerException。有什么问题?

Facelet视图是:

<h:form>
  <p:panel header="FILE UPLOAD WITH CROPPER" style="width:900px; margin: 0 auto; margin-top:0px">
    <p:fileUpload fileUploadListener="#{photoUploadAction.handleImageUpload}"  
      mode="advanced"   
      update="getImageId,messages" auto="false" 
      allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>  

    <p:growl id="messages" showDetail="true"/>
    <p:growl id="uploadMessages" showSummary="true" showDetail="true"/>

    <h:panelGrid columns="2" >
      <p:imageCropper value="#{photoUploadAction.croppedImage}" id="getImageId"
        image="images/#{photoUploadVO.imageName}"/>
    </h:panelGrid>

    <p:commandButton value="Crop" update="getImageId" action="#{imageCropperBean.crop}" />
  </p:panel>
</h:form>

为ImageCropper备份BEAN:

@ManagedBean(name="imageCrop")
@RequestScoped

public class ImageCropperBean {

    private CroppedImage croppedImage;
    private String newFileName;
    private String imageName;


    public String getImageName() {
        return imageName;
    }

    public void setImageName(String imageName) {
        System.out.println("TEH IMAGE NAME ===="+imageName);
        this.imageName = imageName;
    }

    public String getNewFileName() {
        return newFileName;
    }

    public void setNewFileName(String newFileName) {
        System.out.println("AAAAAAAAAAAAAA"+this.newFileName);
        this.newFileName = newFileName;
    }

    public CroppedImage getCroppedImage() {
            return croppedImage;
    }

    public void setCroppedImage(CroppedImage croppedImage) {
        System.out.println("cRRRRRRRRRRRRR"+croppedImage);
            this.croppedImage = croppedImage;
    }

    public ImageCropperBean(){

    }

    public String crop() {
        System.out.println("WELCOMEMMMMMMMMMMMMMM");


        FacesContext context = FacesContext.getCurrentInstance();
        ImageCropperBean imageCropperBean = (ImageCropperBean) context.getApplication().evaluateExpressionGet(context, "#{imageCropperBean}", ImageCropperBean.class);

            ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();

             newFileName = servletContext.getRealPath("") + File.separator + "cropImage"  + File.separator+ "croppedImage.jpg";

             System.out.println("FILE NAME NAME NAME NAME "+newFileName);

            String file = new File(newFileName).getName();
            System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"+file);



             imageCropperBean.setImageName(file);
             File fileFolder = new File("e:/Mecherie_project/image_web/WebContent/cropImages",file);


            System.out.println("FILE ANE"+file);

           // String target=null;

            FileImageOutputStream imageOutput;
            try {
                    imageOutput = new FileImageOutputStream(fileFolder);
                    System.out.println("HHHHHHHHHH=="+imageOutput);
                    imageOutput.write(croppedImage.getBytes(), 0, croppedImage.getBytes().length);
                    imageOutput.close();

                    FacesMessage msg = new FacesMessage("Succesful", file
                            + " is cropped.");

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


            } catch (FileNotFoundException e) {

                FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                        "The files were not Cropped!", "");
                FacesContext.getCurrentInstance().addMessage(null, error);
                e.printStackTrace();

            } catch (IOException e) {
                    e.printStackTrace();

                    FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "The files were not Cropped!", "");
                    FacesContext.getCurrentInstance().addMessage(null, error);
            }

          //  System.out.println("ghfhgfghgh"+target);
            return "success";


            }
}

0 个答案:

没有答案