1

在我的应用程序中,我希望用户创建一个人,提交表单并在下一页验证详细信息,然后提交给 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>

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

投射到人物后阶段 NOT render_response AFter cast Uploaded File Name Is :: 202.JPG Phase NOT render_response AFter cast Uploaded File Size Is :: 0 Phase NOT render_response Uploaded File Name Is :: 202.JPG :: Uploaded File Size :: 0 2015 年 3 月 29 日下午 5:42:15 org.primefaces.application.PrimeResourceHandler handleResourceRequest 严重:流式动态资源中的错误。在 com.myjsf.PersonBean 类型上读取“图像流”时出错

4

2 回答 2

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>

您的图像参数太重。

于 2015-04-06T09:51:39.770 回答
0

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

https://github.com/rdebusscher/AdvancedGraphicImageRenderer

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

于 2015-04-02T13:07:55.383 回答