1 Reply Latest reply on May 14, 2009 5:29 AM by Ilya Shaikovsky

    rich:outputMedia problem

    Ypatios Asmanidis Newbie

      hi, I'm trying to use the fileUpload to upload images .. those images are saved in some folder in my Operating System and I can't access them through a relative path...so I'm using a bean to read this image and display it with the rick:mediaOutput component


      in jsf page I have

       <rich:dataGrid value="#{productHandler.images}" var="img" columns="1" elements="3" width="600px">
       <rich:panel bodyClass="pbody">
       <h:panelGrid columns="2">
       <a4j:mediaOutput createContent="#{img.paint}"/>
      
       <h:outputText value="#{img.name}" />
      
       </h:panelGrid>
       </rich:panel>
       <f:facet name="footer">
       <rich:datascroller></rich:datascroller>
       </f:facet>
       </rich:dataGrid>
      



      the image Entity is

      @Entity
      public class Image implements Serializable{
      
       /**
       *
       */
       private static final long serialVersionUID = 6941126258540487846L;
      
       private long id;
       private String name;
       private String path;
      
       @Id @GeneratedValue
       public long getId() {
       return id;
       }
       public void setId(long id) {
       this.id = id;
       }
       public String getName() {
       return name;
       }
       public void setName(String name) {
       this.name = name;
       }
       public String getPath() {
       return path;
       }
       public void setPath(String path) {
       this.path = path;
       }
      
      
       @Transient
       public void paint(OutputStream out) throws IOException{
      
      
       try {
      
      
       BufferedImage image2D = ImageIO.read(new File(this.path+this.name));
      
       int width = (int) (0.5 * image2D.getWidth());
       int height = (int) (0.5 * image2D.getHeight());
       BufferedImage thumbnail = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
      
      
       Graphics2D bg = thumbnail.createGraphics();
       ImageIO.write(thumbnail, "png", out);
      
       } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
       }
       }
      
      }
      
      
      


      a snapshot of the handler bean is


      
      @DataModel
       private List<Image> images=null;
      
      
      
      
      
       public List<Image> getImages() {
       try{
       System.out.println("size : "+images.size());
       }catch(Exception e) { System.out.println(e.toString());}
       return images;
       }
      
      
      
      
       @PersistenceContext
       private EntityManager em;
      
       @Factory("images")
       public void initImages() {
       if(images==null) images=new ArrayList<Image>();
      
       }
      
       public void photoListener(UploadEvent event) {
       if(images==null) images=new ArrayList<Image>();
       UploadItem item = event.getUploadItem();
       log.info("File : '" + item.getFileName() + "' was uploaded");
       log.info("hi there");
      
      
       if (item.isTempFile()) {
       File file = item.getFile();
       System.out.println("Absolute Path : '" + file.getAbsolutePath() + "'!");
      
       Random random1 = new Random();
       Random random2 = new Random();
      
       int folder=random1.nextInt(100);
       int subfolder=random2.nextInt(100);
      
       if(folder==0) folder=1;
       if(subfolder==0) subfolder=1;
      
       file.renameTo(new File(path+"/"+folder+"/"+subfolder+"/"+item.getFileName()));
       Image buffImage=new Image();
      
       buffImage.setName(item.getFileName());
       buffImage.setPath(path+"/"+folder+"/"+subfolder+"/");
      
       filename=buffImage.getPath()+buffImage.getName();
      
       em.persist(buffImage);
      
       images.add(buffImage);
      
       product.setImages(images);
      
      
      
      
       System.out.println(folder+" - "+subfolder);
      
       //file.delete();
       }
      
      
       }
      
      



      when I try to upload, after the upload is complete I get this error

      09:44:24,530 ERROR [viewhandler] Error Rendering View[/products/newproduct.xhtml]
      javax.faces.FacesException: Required attribute element for component newproduct:j_id59:0:j_id62 is null
       at org.ajax4jsf.renderkit.html.MediaOutputRenderer.doEncodeBegin(MediaOutputRenderer.java:93)
       at org.ajax4jsf.renderkit.RendererBase.encodeBegin(RendererBase.java:101)
       at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:813)
       at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:232)
       at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:178)
       at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:126)
       at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
       at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:282)
       at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
       at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:220)
       at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:215)
       at org.ajax4jsf.renderkit.RendererBase.encodeChildren(RendererBase.java:121)
       at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
       at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:282)
       at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
       at org.richfaces.renderkit.AbstractGridRenderer.encodeOneRow(AbstractGridRenderer.java:96)
       at org.richfaces.renderkit.AbstractRowsRenderer.process(AbstractRowsRenderer.java:86)
       at org.ajax4jsf.model.SequenceDataModel.walk(SequenceDataModel.java:101)
       at org.ajax4jsf.component.UIDataAdaptor.walk(UIDataAdaptor.java:1151)
       at org.richfaces.renderkit.AbstractRowsRenderer.encodeRows(AbstractRowsRenderer.java:106)
       at org.richfaces.renderkit.AbstractRowsRenderer.encodeRows(AbstractRowsRenderer.java:91)
       at org.richfaces.renderkit.AbstractRowsRenderer.encodeChildren(AbstractRowsRenderer.java:138)
       at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
       at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:282)
       at org.ajax4jsf.renderkit.RendererBase.renderChildren(RendererBase.java:262)
       at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:220)
       at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:215)
       at org.ajax4jsf.renderkit.RendererBase.encodeChildren(RendererBase.java:121)
       at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:837)
       at org.ajax4jsf.renderkit.RendererBase.renderChild(RendererBase.java:282)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:124)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxChildren(AjaxChildrenRenderer.java:67)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:115)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxChildren(AjaxChildrenRenderer.java:67)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:115)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxChildren(AjaxChildrenRenderer.java:67)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:115)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxChildren(AjaxChildrenRenderer.java:67)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:115)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxChildren(AjaxChildrenRenderer.java:67)
       at org.ajax4jsf.renderkit.AjaxChildrenRenderer.encodeAjaxComponent(AjaxChildrenRenderer.java:115)
       at org.ajax4jsf.renderkit.AjaxContainerRenderer.encodeAjax(AjaxContainerRenderer.java:123)
       at org.ajax4jsf.component.AjaxViewRoot.encodeAjax(AjaxViewRoot.java:677)
       at org.ajax4jsf.component.AjaxViewRoot.encodeChildren(AjaxViewRoot.java:548)
       at javax.faces.component.UIComponent.encodeAll(UIComponent.java:936)
       at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592)
       at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
       at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:196)
       at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:109)
       at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100)
       at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
       at javax.faces.webapp.FacesServlet.service(FacesServlet.java:266)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
       at org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       at org.jboss.seam.web.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:42)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:177)
       at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:267)
       at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:380)
       at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:507)
       at org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       at org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:58)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       at org.jboss.seam.web.HotDeployFilter.doFilter(HotDeployFilter.java:53)
       at org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
       at org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
       at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:173)
       at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:433)
       at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
       at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
       at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
       at java.lang.Thread.run(Thread.java:636)
      
      
      



      what am I doing wrong and is there another way to access photos and files...and display them to my pages?