Wicket图像组件未在页面上找到

时间:2011-04-19 09:24:43

标签: java wicket

尝试显示项目列表时出现以下问题。对于每个项目,我必须显示通过Wicket WebResource动态加载的图像。在用户滚动时,使用Ajax滚动逐步加载项目 - 每次50个。

  

[ERROR] 2011-04-19 09:58:18,000 btpool0-1 org.apache.wicket.RequestCycle.logRuntimeException(host =,request =,site =):   org.apache.wicket.WicketRuntimeException:component documentList:scroller:batchElem:666:content:item:3:batchItemContent:linkToPreview:imageThumbnail at page com.webapp.document.pages.DocumentListPage [id = 1]   listener interface = [RequestListenerInterface name = IResourceListener,method = public abstract void org.apache.wicket.IResourceListener.onResourceRequested()]

     

org.apache.wicket.protocol.http.request.InvalidUrlException:org.apache.wicket.WicketRuntimeException:component documentList:scroller:batchElem:666:content:item:3:batchItemContent:linkToPreview:imageThumbnail   页面上没有找到com.webapp.document.pages.DocumentListPage [id = 1] listener interface = [RequestListenerInterface name = IResourceListener,method = public abstract void org.apache.wicket.IResourceListener.onResourceRequested()]       在org.apache.wicket.protocol.http.WebRequestCycleProcessor.resolve(WebRequestCycleProcessor.java:262)       在org.apache.wicket.RequestCycle.step(RequestCycle.java:1310)       在org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)       在org.apache.wicket.RequestCycle.request(RequestCycle.java:545)       在org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)       在org.apache.wicket.protocol.http.WicketFilter $$ EnhancerByGuice $$ 51619816.CGLIB $ doGet $ 6()       在org.apache.wicket.protocol.http.WicketFilter $$ EnhancerByGuice $$ 51619816 $$ FastClassByGuice $$ 6d42bf5d.invoke()       在com.google.inject.internal.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)       在com.google.inject.internal.InterceptorStackCallback $ InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:64)       在com.freiheit.monitoring.PerformanceMonitoringMethodInterceptor.invoke(PerformanceMonitoringMethodInterceptor.java:115)       在com.google.inject.internal.InterceptorStackCallback $ InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:64)       在com.google.inject.internal.InterceptorStackCallback.intercept(InterceptorStackCallback.java:44)       在org.apache.wicket.protocol.http.WicketFilter $$ EnhancerByGuice $$ 51619816.doGet()       在org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:312)       在org.apache.wicket.protocol.http.WicketFilter $$ EnhancerByGuice $$ 51619816.CGLIB $ doFilter $ 4()

如何解决这个问题?

以下是负责添加图片的代码部分:

previewLink.add(createThumbnailSmall("imageThumbnail", documentModel));

createThumbnailSmall(final String id, final IModel<BaseDocument> documentModel) {
    // thumbnailResource is an object that contains the path of the image

    if (thumbnailResource != null) {
        final WebResource resource = getWebResource(thumbnailResource);
        final Image image = new Image(id, resource);
        return image;
    }
    return new InvisibleContainer(id);
}

WebResource getWebResource(final DocumentResource documentResource) {
    return new WebResource() {

        private static final long serialVersionUID = 1L;

        @Override
        public IResourceStream getResourceStream() {
            return new BaseStreamResource(documentResource);
        }
    };
}

其中BaseStreamResource如下:

public class BaseStreamResource extends AbstractResourceStream {
    private InputStream      _fileInputStream = null;
    private DocumentResource _resource        = null;

    public BaseStreamResource(final DocumentResource documentResource) {
        _resource = documentResource;
    }

    @Override
    public InputStream getInputStream() throws ResourceStreamNotFoundException {
        if (_fileInputStream == null) {
            try {
                if (_resource == null) {
                    throw new ResourceStreamNotFoundException("Resource was null");
                }
                _fileInputStream = _resource.getFileInputStream();
            } catch (final ResourceNotAvailableException ex) {
                throw new ResourceStreamNotFoundException(ex);
            }
        }
        return _fileInputStream;
    }

在HTML中:

<a wicket:id="linkToPreview" href="#">
<img wicket:id="imageThumbnail" alt="Attachment"></img></a>

2 个答案:

答案 0 :(得分:1)

添加的代码并没有真正为我添加任何线索,但也许我可以帮助缩小范围。

stacktrace包含对com.webapp.document.pages.DocumentListPage的引用,它可能会调用您发布的一些代码。该错误表示错误的URL,因此调试该类,添加调试打印,以及查看包含url的任何字段的值可能是值得的。

甚至可能有助于修改DocumentListPage中的代码(可能暂时用于调试)以捕获org.apache.wicket.protocol.http.request.InvalidUrlException并在捕获异常时专门添加调试打印。

这不是一个真正的答案,但它对评论来说太大了,也许它会帮助你更接近答案。

答案 1 :(得分:1)

以下解决方案解决了这个问题:
- 扩展WebResource类
- 将扩展类作为资源添加到应用程序共享资源

例如:

public class MyWebResource extends WebResource {
    final ValueMap map = new ValueMap();

    @Override
    public IResourceStream getResourceStream() {        
        String fileName = getFileName();
        File file = new File(basePath, fileName);

        if (!file.exists()) {
            LOG.error("File does not exist: " + file);
            throw new IllegalStateException("File does not exist: " + file);
        }       
        return new FileResourceStream(file);
    }           

    public final void addResource() {
        Application.get().getSharedResources().add(getClass().getName(), this);
    }

    protected String getFileName() {
        return getParameters().getString("id");
    }   

    public final String urlFor(final String fileName) {         
        final ResourceReference resourceReference = new ResourceReference(getClass().getName());        
        final String encodedValue = WicketURLEncoder.QUERY_INSTANCE.encode(fileName);
        map.add("id", encodedValue);
        final CharSequence result = RequestCycle.get().urlFor(resourceReference, map);       
        if (result == null) {
            throw new IllegalStateException("The resource was not added! "
                + "In your Application class add the following line:"
                + "MyConcreteResource.INSTANCE.addResource()");
        }   

        String absoluteUrl = RequestUtils.toAbsolutePath(result.toString());        
        return absoluteUrl;     
    } 
}

在Application类中,在init()中,我已将MyWebResource添加到共享资源:

public void init() {
    ... 
    new MyWebResource().addResource();        
    ...
}