每次重新加载页面时都会多次挂载Wicket ImageResourceReference

时间:2014-02-20 11:41:10

标签: java web-applications wicket

我正在尝试在我的页面上安装ImageResourceReference,但是ExternalLink已经多次挂载(每次我重新加载页面时,我都会得到一个新的附加链接(同一个)。 例如,当我第一次启动服务器并加载页面时,只有一个ExternalLink,第二次,两个链接,第三次三个等等......

可能是什么原因?

这是我的代码: WebApp.java:

void init() {
.....
mountResource("/book/number/${number}/images/ray/${name}", new ImageResourceReference());
....
}

ImageResourcesPanel:

public class ImageResourcesPanel extends Panel {

    private static final long serialVersionUID = -8723530004274531683L;
    private static Logger logger = LoggerFactory.getLogger(ImageResourcesPanel.class
            .getName());

    /**
     * The image names for which dynamic images will be generated
     */
    private static List<String> IMAGE_NAMES = new ArrayList<String>(); 


    public ImageResourcesPanel(final String wicketId, final IModel<Device> model) {
        super(wicketId, model);

        String pathToImage = "images";
        IMAGE_NAMES.add(pathToImage);

        ListView<String> listView = new ListView<String>("list", IMAGE_NAMES) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(ListItem<String> item) {

                logger.debug("Executed!");

                ResourceReference imagesResourceReference = new ImageResourceReference();
                PageParameters imageParameters = new PageParameters();


                int number = model.getObject().getNumber();
                String imageName = item.getModelObject();
                String folder = model.getObject().getLinkToFolder();
                imageParameters.set("name", imageName);
                imageParameters.set("number", number);
                imageParameters.set("folder", folder);

                // generates nice looking url (the mounted one) to the current image
                CharSequence urlForWordAsImage = getRequestCycle().urlFor(imagesResourceReference, imageParameters);
                ExternalLink link = new ExternalLink("link", urlForWordAsImage.toString());
                link.setBody(Model.of(imageName));
                item.add(link);

            }
        };
        add(listView);
    }
}

1 个答案:

答案 0 :(得分:0)

知道了!

我必须在页面上安装图像后将ListView设为空。我在添加ListView-Item的链接后添加了一行代码:

`IMAGE_NAMES.remove(pathToImage);`