如何使用数据库中的primefaces在datatable中加载动态图像?图像未显示在数据表中

时间:2014-06-17 23:25:09

标签: java jsf java-ee file-upload primefaces

我正在使用primfaces 5版本来显示我的数据库中的数据库中的图像,但不幸的是,而不是实际的图像"图像"正在写入列,这是我的xhtml文件。 我有一个带有StreamedContent属性的对象列表,我需要在组件中显示它。 我抛出没有结果的showcase primefaces

  <p:dataTable id="dta" value="#{UtilisateurComponent.listUtilisateurs()}"  var="current" rows="15" paginator="true" paginatorPosition="bottom" rowKey="current.iduser">
        <p:column> 
                    <f:facet name="header">
                        <h:outputText value="id User" />
                    </f:facet>
                    <h:outputText value="#{current.iuser}"  />
        </p:column>
        <p:column > 
                    <f:facet name="header">
                        <h:outputText value="Photo" />
                    </f:facet>
                        <p:graphicImage  value="#{UtilisateurComponent.content}" height="100" width="150">
                            <f:param name="id" value="#{current.iduser}" />
                        </p:graphicImage>
        </p:column>
  </p:dataTable>

这是我的托管bean:

enter code here
        @Scope("session")
        @Component("UtilisateurComponent")
        public class UtilisateurComponentImpl implements UtilisateurComponent {

        private Utilisateur utilisateur;
        byte[] f;
        private UploadedFile file;
        private StreamedContent content;
        @Autowired
        private UtilisateurDAO utilisateurDAO;
        @Autowired
        private UtilisateurService utilisateurService;
        public StreamedContent getContent() {
          FacesContext context = FacesContext.getCurrentInstance();

           if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
                     return new DefaultStreamedContent();
                 }
           else{
            String imageId = context.getExternalContext().getRequestParameterMap().get("id");
           Utilisateur u = utilisateurDAO.findUtilisateurByIduser(Integer.parseInt(imageId));
           return new DefaultStreamedContent(new ByteArrayInputStream(u.getPhoto())); //the line that throw the Error 
           }
         }

     public void setContent(StreamedContent content) {
        this.content = content;
    }

    @PostConstruct
     public void init() {
         //initialize the data here
         this.utilisateur = new Utilisateur();

     }


     public UploadedFile getFile() {
         return file;
     }
     public void setFile(UploadedFile file) {
         this.file = file;
     }


       @Transactional
     public String saveUtilisateurTest(Utilisateur utilisateur) {
      try{

           utilisateur.setPhoto(getFile().getContents());

           utilisateurService.saveUtilisateur(utilisateur);
           file=null;

          }
          catch(Exception ex){
           ex.printStackTrace();
          }
         return "/jsf/utilisateur/listUtilisateurs.xhtml";

     }
     f = new byte[event.getFile().getContents().length];
      System.arraycopy(event.getFile().getContents(),0,f,0,event.getFile().getContents().length);

          FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");

          FacesContext.getCurrentInstance().addMessage(null, message);

     }
}

根据我的堆栈跟踪,

Grave: Error in streaming dynamic resource. Error reading 'content' on type $Proxy16
juin 17, 2014 10:56:03 PM org.apache.catalina.core.StandardWrapperValve invoke
Grave: "Servlet.service()" pour la servlet Faces Servlet a généré une exception
java.io.IOException: javax.el.ELException: Error reading 'content' on type $Proxy16
    at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:122)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:98)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.el.ELException: Error reading 'content' on type $Proxy16
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:110)
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:71)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
    at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:85)
    ... 27 more
Caused by: java.lang.NullPointerException
    at java.io.ByteArrayInputStream.<init>(Unknown Source)
    at com.gestion.projet.web.jsf.UtilisateurComponentImpl.getContent(UtilisateurComponentImpl.java:119)
    at sun.reflect.GeneratedMethodAccessor38.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

该行(UtilisateurComponentImpl.java:119)在managedbean方法上发表了评论

StreamedContent getMyContent()

2 个答案:

答案 0 :(得分:0)

return new DefaultStreamedContent(new ByteArrayInputStream(u.getPhoto()));

在上面的行中,你是null,问题是你没有把他从DataBase带回来,或者你没有在bean中接收到imageId。

你可以这样做

<p:graphicImage  value="#{UtilisateurComponent.getContent(current.iduser)}" height="100" width="150"/>

在您的功能中,只需获取您的用户ID

即可
public StreamedContent getContent(Integer userId){...}

希望这会有所帮助

答案 1 :(得分:-1)

问题在于传递了param,你有一个包含多行的dataTable然后你发送具有相同id的params。