流动态资源时出错.空值

编程入门 行业动态 更新时间:2024-10-28 16:30:43
本文介绍了流动态资源时出错.空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

重要通知::此问题已已修复,自 PrimeFaces 5.2 final (社区发布)已发布于2015年4月8日.因此,如果您碰巧使用了该版本或较新,则无需摆弄临时的解决方法.

Important Notice : This issue has been fixed as of PrimeFaces 5.2 final (Community Release) released on April 8, 2015. As such if you happened to use that version or newer, you would not need to fiddle around with a temporary workaround.

前面给出的示例现在可以安全地进行如下修改.

The earlier given example can now safely be modified as follows.

public StreamedContent getImage() throws IOException { FacesContext context = FacesContext.getCurrentInstance(); if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { return new DefaultStreamedContent(); } else { String id = context.getExternalContext().getRequestParameterMap().get("id"); byte[] bytes = Utils.isNumber(id) ? service.findImageById(Long.parseLong(id)) : null; return bytes == null ? null : new DefaultStreamedContent(new ByteArrayInputStream(bytes)); } }

在我厌倦了操纵/管理磁盘文件系统中存储的图像之后,我已经以BLOB(LONGBLOB)的形式将图像移至数据库(MySQL).

I have moved images to the database (MySQL) in the form of BLOB (LONGBLOB) after I got tired of manipulating/managing images stored in the disk file system.

因此,我将在<p:dataTable>中显示图像,如下所示(从此处:复制)). /p>

Accordingly, I'm displaying images in <p:dataTable> as follows (blatantly copied from here :) ).

<p:column headerText="Header"> <p:graphicImage value="#{bannerBean.image}" height="200" width="200"> <f:param name="id" value="#{row.bannerId}"/> </p:graphicImage> <p:column>

检索图像的bean如下.

The bean that retrieves images is as follows.

@ManagedBean @ApplicationScoped public final class BannerBean { @EJB private final BannerBeanLocal service=null; public BannerBean() {} public StreamedContent getImage() throws IOException { FacesContext context = FacesContext.getCurrentInstance(); if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) { return new DefaultStreamedContent(); } else { String id = context.getExternalContext().getRequestParameterMap().get("id"); byte[] bytes = service.findImageById(Long.parseLong(id)); return bytes==null? new DefaultStreamedContent():new DefaultStreamedContent(new ByteArrayInputStream(bytes)); } } }

只要基础数据库表的每一行中都有图像,此方法就可以正常工作.

This works fine as long as there are images in each row of the underlying database table.

但是,数据库中的BLOB类型列在某些情况下是可选的,因此它也可以包含null值.

The BLOB type column in the database is however, optional in some cases and hence, it can contain null values as well.

如果数据库中任何行中的此列为null,则将引发以下异常.

If this column in any row/s in the database is null then, the following exception is thrown.

SEVERE: Error in streaming dynamic resource. null WARNING: StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception java.lang.NullPointerException at org.primefaces.application.PrimeResourceHandler.handleResourceRequest(PrimeResourceHandler.java:127) at javax.faces.application.ResourceHandlerWrapper.handleResourceRequest(ResourceHandlerWrapper.java:153) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:643) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) at java.lang.Thread.run(Thread.java:722)

那么如何管理null BLOB,使该异常消失?

So how to manage null BLOBs so that this exception disappears?

在受管bean中的字节数组为null的情况下返回new DefaultStreamedContent(new ByteArrayInputStream(new byte[0]))会抑制该异常,但这毕竟不是解决方案.这是理想的解决方案吗?

Returning new DefaultStreamedContent(new ByteArrayInputStream(new byte[0])), in case, the byte array in the managed bean is null would suppress the exception but this after all, should not be a solution. Is this a desired solution?

在这种情况下,虽然完全不需要,但返回字节数组的EJB方法.

The EJB method that returns a byte array though completely unnecessary, in this case.

public byte[] findImageById(Long id) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<byte[]>criteriaQuery=criteriaBuilder.createQuery(byte[].class); Root<BannerImages> root = criteriaQuery.from(BannerImages.class); criteriaQuery.multiselect(root.get(BannerImages_.bannerImage)); ParameterExpression<Long>parameterExpression=criteriaBuilder.parameter(Long.class); criteriaQuery.where(criteriaBuilder.equal(root.get(BannerImages_.bannerId), parameterExpression)); List<byte[]> list = entityManager.createQuery(criteriaQuery).setParameter(parameterExpression, id).getResultList(); return list!=null&&!list.isEmpty()?list.get(0):null; }

推荐答案

这是 PrimeResourceHandler .它不应该假定动态资源或其内容永远都不是null.它应该有条件地检查是否是这种情况,然后只需返回HTTP 404未找到"响应即可.

This is a bug (at least, a functional/technical design mistake) in PrimeResourceHandler. It shouldn't have assumed the dynamic resource or its content to be never null. It should have conditionally checked if that was the case and then simply have returned a HTTP 404 "Not Found" response.

换句话说,代替

85 streamedContent = (StreamedContent) ve.getValue(eLContext); 86 87 externalContext.setResponseStatus(200); 88 externalContext.setResponseContentType(streamedContent.getContentType());

他们应该做的

85 streamedContent = (StreamedContent) ve.getValue(eLContext); 86 87 if (streamedContent == null || streamedContent.getStream() == null) { 88 externalContext.responseSendError(HttpServletResponse.SC_NOT_FOUND, ((HttpServletRequest) externalContext.getRequest()).getRequestURI()); 89 return; 90 } 91 92 externalContext.setResponseStatus(200); 93 externalContext.setResponseContentType(streamedContent.getContentType());

这样,您只需从getImage()方法返回null或空的StreamedContent即可生成体面的404.

This way you can just return null or an empty StreamedContent from the getImage() method in order to generate a decent 404.

那么,你能做什么?

  • 举报,并希望他们能解决它. 更新他们已在版本5.2中对其进行了修复.

  • Report it to them and hope that they'll fix it. Update They fixed it in version 5.2.

    和/或,放置PrimeResourceHandler类的="nofollow">副本中,完全使用其自己的org.primefaces.application包,然后对其进行编辑以包括上述更改,最后仅构建/部署您的像往常一样,将WAP作为webapp项目.

    And/or, put a copy of PrimeResourceHandler class in the Java source folder of your webapp project, in exactly its own org.primefaces.application package and then just edit it to include the mentioned change and finally just build/deploy your webapp project as WAR as usual. Classes in /WEB-INF/classes have higher classloading precedence over those in JARs in /WEB-INF/lib, so the modified one will be used instead.

  • 更多推荐

    流动态资源时出错.空值

    本文发布于:2023-11-24 11:22:27,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1625004.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:资源

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!