3 Replies Latest reply on Nov 3, 2011 10:22 AM by sergejzr

    a4j:mediaOutput and jsf2 view scope

    bliof

      I am trying to upload and show an image just as in the richfaces showcase.

      The upload part works fine, but I am having some problems with the ajax that should show the image.

       

       

      This is the xhtml part:

       

       

          <h:form>
                                    <h:panelGroup id="profPic" layout="block">
                                              <a4j:mediaOutput element="img" mimeType="image/png"
                                                        createContent="#{imageUpload.paint}"
                                                        style="width:100px; height:100px;" cacheable="false">
                                                        <f:param value="#{imageUpload.timeStamp}" name="time" />
                                              </a4j:mediaOutput>
                                    </h:panelGroup>
                                    <rich:fileUpload fileUploadListener="#{imageUpload.listener}"
                                              id="imageUploader" acceptedTypes="jpg, gif, png, bmp"
                                              maxFilesQuantity="1" addLabel="Browse">
                                              <f:ajax event="uploadcomplete" execute="@none" render="profPic"></f:ajax>
                                    </rich:fileUpload>
                          </h:form>
      
      

       

      This is my imageUploader bean:

       

          @ManagedBean
          @ViewScoped
          public class ImageUpload {
                private UploadedFile file;
      
      
                public void paint(OutputStream stream, Object object) throws IOException {
                          if (file.getData() != null) {
                                    stream.write(file.getData());
                                    stream.close();
                          }
                }
      
      
                public void listener(FileUploadEvent event) throws Exception {
                          file = event.getUploadedFile();
                }
      
      
                public String clearUploadData() {
                          file = null;
                          return null;
                }
      
      
                public int getSize() {
                          if (file != null) {
                                    return 1;
                          }
                          return 0;
                }
      
      
                public long getTimeStamp() {
                          return System.currentTimeMillis();
                }
      
      
                public void setFile(UploadedFile file) {
                          this.file = file;
                }
      
      
                public UploadedFile getFile() {
                          return file;
                }
          }
      

       

      If the bean is in the view scope, the paint method won't be started. But if it is in the session scope, it will. In both scopes the timeStamp method is started.

      Why is this happening? And how to make it work in view scope?

        • 1. Re: a4j:mediaOutput and jsf2 view scope
          chantal.khoury

          Hello,

           

          I'm facing the same issue on mediaOutput when paint method is called from a bean that is declared in view scope.

          Here is the stacktrace generated:

           

          E com.ibm.ws.webcontainer.webapp.WebApp logServletError SRVE0293E: [Erreur du servlet]-[Faces Servlet] : java.lang.NullPointerException

              at com.sun.faces.mgbean.BeanManager$ScopeManager$ViewScopeHandler.getFromScope(BeanManager.java:563)

              at com.sun.faces.mgbean.BeanManager$ScopeManager.getFromScope(BeanManager.java:477)

              at com.sun.faces.mgbean.BeanManager.getBeanFromScope(BeanManager.java:240)

              at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:242)

              at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)

              at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)

              at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)

              at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:99)

              at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)

              at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)

              at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:95)

              at com.sun.el.parser.AstValue.getTarget(AstValue.java:127)

              at com.sun.el.parser.AstValue.invoke(AstValue.java:217)

              at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)

              at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)

              at org.richfaces.resource.MediaOutputResource.encode(MediaOutputResource.java:71)

              at org.richfaces.resource.UserResourceWrapperImpl.encode(UserResourceWrapperImpl.java:193)

              at org.richfaces.resource.ResourceHandlerImpl.handleResourceRequest(ResourceHandlerImpl.java:226)

              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:310)

              at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1663)

              at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)

              at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)

              at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:179)

              at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3933)

              at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)

              at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:931)

              at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1583)

              at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)

              at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)

              at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)

              at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)

              at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:83)

              at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)

              at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)

              at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)

              at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)

              at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)

              at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)

              at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)

              at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1604)

           

          Thanks for your help

          • 2. Re: a4j:mediaOutput and jsf2 view scope
            sergejzr

            Hi guys, i have the same problem. mediaOutput only works in @SessionScope and @ApplicationScope, not in @ViewScope. Please answer how you have solved this problem..

            • 3. Re: a4j:mediaOutput and jsf2 view scope
              sergejzr

              Here is how I solved this problem.

               

              I have a bean MyBean, which is @VewScoped. I created new bean ImageBean which is @SessionScoped

               

              in my bean I add the ImageBean as a property by:

               

              MyBean

              @ManagedBean
              @ViewScoped
              public class MyBean implements Serializable {
                 
                  @ManagedProperty(value="#{imageBean}")
                  private ImageBean imageBean;
                  
                  
                  public ImageBean getImageBean() {
                      return imageBean;
                  }
              
                  public void setImageBean(ImageBean imageBean) {
                      this.imageBean = imageBean;
                  }
              
              /*
              For the file upload:
              */
              public void listener(FileUploadEvent event) throws Exception {
                      UploadedFile uploadedFile = event.getUploadedFile();
                      imageBean.putPic(uploadedFile);
                  }
              public void upolad(){}
              public void save(){}
              }
              

               

              ImageBean:

              @ManagedBean
              @SessionScoped
              public class ImageBean
              {
              
              UploadedFile> file;
              
              public void putPic(UploadedFile file) {
                  this.file=file;
              }
              
              
              public UploadedFile getFile() {
                  return file;
              }
              
              public void paint(OutputStream out, Object data) throws IOException {
                  
                  
                  int width = 0, height = 0;
                  if (data instanceof MediaData) {
                      MediaData paintData = (MediaData) data;
                      width = paintData.getWidth();
                      height = paintData.getHeight();
                  } else {
                      width = 200;
                      height = 200;
                  }
                  BufferedImage imgup = ImageIO.read(file.getInputStream());
                  ImageIO.write(imgup, "jpeg", out);
              }
              
              }
              

               

               

              now the faces part:

               

              <rich:fileUpload fileUploadListener="#{myBean.listener}"
                                      id="upload" addLabel="Select file"
                                      onfilesubmit="#{myBean.save()}"
                                      oncomplete="#{myBean.upload()}"
                                       immediateUpload="#{false}"/ >
              
              
              
              <rich:panel styleClass="richpanel" rendered="#{ImageBean.getFile()!=null}">
              <a4j:mediaOutput style="max-width: 200px;max-height: 200px" element="img" cacheable="false" session="true"
                      createContent="#{imageBean.paint}" value="#{mediaData}" mimeType="image/jpeg" />
              </rich:panel>
              

               

               

              Iknow, it is a dirty workaround, not a clean solution. But it seems to be the only one available at the moment...