Richfaces:a4j:媒体输出,数据为'null'

时间:2013-09-17 15:10:47

标签: java file-upload richfaces output

当我尝试显示上传的图像时,我得到了nullpointer。

我按照以下示例进行操作:Richfaces Showcase

错误: javax.el.E​​LException:... [path] ... createContent =“#{imageBean.paint}”:java.lang.NullPointerException

使用过的源代码:

ImageBean.java

@ManagedBean
@SessionScoped
public class ImageBean implements Serializable{
  private ImageData imageData= new ImageData();

  public void listener(FileUploadEvent event) throws Exception {
    UploadedFile item = event.getUploadedFile();
    imageData = new ImageData();
    imageData.setName(item.getName());
    imageData.setData(item.getData());
  }

  public void paint(OutputStream stream, Object object) throws IOException {
    stream.write(getFile().getData());
    stream.close();
  }

  // getters and setters for file

uploadImage.xhtml

<h:form>
        <h:panelGrid columns="2" columnClasses="top,top">
            <rich:fileUpload fileUploadListener="#{imageBean.listener}" id="upload" acceptedTypes="jpg, gif, png, bmp"
                ontyperejected="alert('Only JPG, GIF, PNG and BMP files are accepted');" maxFilesQuantity="1">
                <a4j:ajax event="uploadcomplete" execute="@none" render="info" />
            </rich:fileUpload>
            <h:panelGroup id="info" layout="block">
                <rich:panel bodyClass="info">
                    <f:facet name="header">
                        <h:outputText value="Uploaded Files Info" />
                    </f:facet>
                    <h:outputText value="Geen foto beschikbaar" rendered="#{imageBean.size==0}" />
                    <a4j:mediaOutput element="img" mimeType="image/jpeg" createContent="#{imageBean.paint}"
                        value="#{imageBean.file.name}" style="width:100px; height:100px;" cacheable="false">
                    </a4j:mediaOutput>
                    <h:panelGrid columns="2">
                        <h:outputText value="File Name:" />
                        <h:outputText value="#{imageBean.file.name}" />
                    </h:panelGrid>
                </rich:panel>
                <br />
            </h:panelGroup>
        </h:panelGrid>
        </h:form>

根据我的调试器: imageDate在上传侦听器()中获取数据 但在 paint()

中为空

我的想法是,我应该把我的范围扩大。 所以我将其设置为 @SessionScoped

Web.xml中

<context-param>
  <param-name>createTempFiles</param-name>
  <param-value>false</param-value>
 </context-param>

我尝试了真假

使用Richfaces:4.3.2

1 个答案:

答案 0 :(得分:0)

您尝试在未上传文件时呈现图片。 <a4j:mediaOutput>需要有条件地呈现。