在我的应用程序中,我希望用户创建一个人,提交表单并在下一页验证详细信息,然后提交给 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 类型上读取“图像流”时出错