在提交到数据库primefaces之前,在p:graphicImage中预览图像

时间:2015-03-29 23:10:01

标签: file-upload primefaces jsf-2.2 graphicimage

在我的应用程序中,我希望用户创建一个人,提交表单并在下一页上验证详细信息,然后提交给db。这个人也可以有一个图像。为简单起见,我只使用两个字段1)名称2)文件。我无法在"验证"上显示图像。页。 bean是会话范围的,但是当FacesContext.getCurrentInstance()不等于PhaseId.RENDER_RESPONSE时,文件大小变为0.我是一个JSF新手,即使在理解有2个单独的调用之后也无法弄清楚使用p:graphicImage

Testfileupload.xhtml位于

之下

        <h:outputText value="Person Name *" />
        <p:inputText value="#{personBean.name}"
                        required="true" 
                        requiredMessage="You must enter a Business Name"
                        id="name" />
        <p:message for="name" />

        <h:outputLabel for="image" value="Select Picture:" />
        <p:fileUpload id="image" value="#{personBean.file}" mode="simple" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
        <p:message for="image" />

        <p:commandButton value="Submit" action="#{personBean.addverifyBusiness}" ajax="false" /> 
</h:form>

PersonBean.java位于

之下
public class PersonBean {

private String name;
private UploadedFile file;
private Person person = new Person();
private StreamedContent imagestream;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

public Person getPerson() {
    return person;
}

public void setPerson(Person person) {
    this.person = person;
}

public StreamedContent getImagestream() {

    System.out.println("inside getImagestream() Uploaded File Name Is :: "+file.getFileName()+" :: Uploaded File Size :: "+file.getSize());
    if (file != null) {
        System.out.println("file is not null");
        FacesContext context = FacesContext.getCurrentInstance();
        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
            System.out.println("phase = render_response Uploaded File Name Is :: "+file.getFileName()+" :: Uploaded File Size :: "+file.getSize());
            imagestream = new DefaultStreamedContent();
            return imagestream;
        } else {
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            Map<String, Object> sessionMap = externalContext.getSessionMap();
            for(String key: sessionMap.keySet())
                System.out.println(key + " - " + sessionMap.get(key));
                System.out.println();
            PersonBean personBean = (PersonBean) sessionMap.get("personBean");
            Person person = personBean.getPerson();
            System.out.println("After casting to person");
            String filenamenew = person.getFile().getFileName();
            System.out.println("phase NOT render_response AFter cast Uploaded File Name Is :: "+filenamenew);
            Long filesize = person.getFile().getSize();
            System.out.println("phase NOT render_response AFter cast Uploaded File Size Is :: "+filesize);
            UploadedFile file1 = person.getFile();  
            System.out.println("phase NOT render_response Uploaded File Name Is :: "+file1.getFileName()+" :: Uploaded File Size :: "+file1.getSize());
            return new DefaultStreamedContent(new ByteArrayInputStream(file1.getContents()));
        } 

    } else {
        System.out.println("file is null");
        return new DefaultStreamedContent();
    }
}

    public void setImagestream(StreamedContent imagestream) {
          this.imagestream = imagestream;
       }
public String addverifyBusiness() {
    person.setName(name);
    person.setFile(file);
    return("testresult");  // Means to go to testresult.xhtml (since condition is not mapped in faces-config.xml)
}

}

Person.java位于

之下
public class Person {
    private String name;
    private UploadedFile file;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
    System.out.println("inside Person setfile Uploaded File Name Is :: "+file.getFileName()+" :: Uploaded File Size :: "+file.getSize());
}

}

testresult.xhtml位于

之下
<h:body>
     <h:outputText value="Picture" />
                    <p:graphicImage value="#{personBean.imagestream}" >
                    <f:param name="file" value="#{personBean.person.file}" />
                    </p:graphicImage>


  </h:body>

结果如下 inside Person setfile上传文件名是:: 202.JPG :: Uploaded File Size :: 2022045 inside getImagestream()uploaded File Name Is :: 202.JPG :: Uploaded File Size :: 2022045 file不为null phase = render_response上传的文件名是:: 202.JPG ::上传的文件大小:: 2022045 inside getImagestream()上传的文件名是:: 202.JPG ::上传的文件大小:: 0 file不为null javax.faces.request.charset - UTF-8 personBean - com.myjsf.PersonBean@2784a989

铸造后的人 阶段NOT render_response AFter强制上传文件名是:: 202.JPG 阶段NOT render_response AFter强制上传文件大小是:: 0 阶段NOT render_response上传的文件名是:: 202.JPG ::上传的文件大小:: 0 2015年3月29日下午5:42:15 org.primefaces.application.PrimeResourceHandler handleResourceRequest 严重:流动态资源出错。读取&#39;图像流&#39;时出错在类型com.myjsf.PersonBean

2 个答案:

答案 0 :(得分:1)

将您的public StreamedContent getImagestream() { ... }更改为此

public StreamedContent getImagestream2() {
    if( file != null ){
        return new DefaultStreamedContent(new ByteArrayInputStream(file.getContents()), file.getContentType());
    }else{
        return new DefaultStreamedContent();
    }
}

在结果视图中,使用:

<p:graphicImage value="#{personBean.imagestream2}" >
                    <f:param name="file" value="#{personBean.person.file.fileName}" />
                </p:graphicImage>

您的图片参数太重了。

答案 1 :(得分:0)

我认为primefaces只能渲染静态内容,即使用默认的pgraphic图像。如果您希望在执行任何其他操作之前在验证页面上呈现图像,则需要某种类型的额外组件。我会推荐AdvancedGraphicImageRenderer。我将在下面附上链接。它没有任何问题,对我有用。代码几乎是自我解释并且很容易修改。

https://github.com/rdebusscher/AdvancedGraphicImageRenderer

我忘了提到你可能需要创建一个处理字节数组的org.primefaces.model.StreamedContent实现。如果我没记错,我不认为默认的流内容是可序列化的。我在开发webflow应用程序时遇到了这个错误。如果需要,我可以检查一下。我想我还有代码。