从WicketPanel传递PageParameters

时间:2014-02-11 10:09:42

标签: java image wicket panel mount

我如何在Wicketpanel上使用PageParameters? 我愿意在WicketPanel上从文件系统加载图像,我找到了一个教程,但他们使用的是一个Page,在我的例子中,我想在Panel上安装图像。我应该在这个类中更改什么,或者我是否必须为此用例实现PageClass? http://wicketinaction.com/2011/07/wicket-1-5-mounting-resources/ https://github.com/martin-g/blogs/blob/master/request-mappers/src/main/java/com/wicketinaction/requestmappers/resources/images/ImageResourcesPage.java

public class ImageResourcesPage extends WebPage {

    /**
     * The image names for which dynamic images will be generated
     */
    private static final String[] IMAGE_NAMES = new String[] {"one", "two", "three"};

    public ImageResourcesPage(final PageParameters parameters) {
        super(parameters);

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

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

            @Override
            protected void populateItem(ListItem<String> item) {
                String imageName = item.getModelObject();
                imageParameters.set("name", imageName);

                // 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);
    }

}

由于

3 个答案:

答案 0 :(得分:1)

显示的示例代码实际上根本没有使用图片处理中pageParameters的{​​{1}},但是图片还有一个WebPage字段。没有理由你不能在PageParameters imageParameters中做同样的事情。

的内容
Panel

应该与显示的页面版本一样好。

我不确定public class ImageResourcesPanel extends Panel { /** * The image names for which dynamic images will be generated */ private static final String[] IMAGE_NAMES = new String[] {"one", "two", "three"}; public ImageResourcesPanel(final String wicketId) { super(wicketId); final ResourceReference imagesResourceReference = new ImageResourceReference(); final PageParameters imageParameters = new PageParameters(); ListView<String> listView = new ListView<String>("list", Arrays.asList(IMAGE_NAMES)) { @Override protected void populateItem(ListItem<String> item) { String imageName = item.getModelObject(); imageParameters.set("name", imageName); // 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); } } imageParameters的最终字段是否合适。我可能只是在imageResourceReference方法中使它们成为局部变量。

根据评论进行更新:

看来这个例子产生了图像的链接,你想要的是嵌入的图像。更好的起点可能是wicket-library示例页面中的images example。该 但是,此示例中的populateItem(ListItem<String> item)代码可能仍然可以与其他示例结合使用。

答案 1 :(得分:0)

您不能只是将您嵌入的面板/页面中的页面参数移交给它吗? 或者你可以使用

RequestCycle.get().getPageParameters()

但我认为将其从嵌入组件中移除会更清晰。

答案 2 :(得分:0)

知道了!我只是想在ImageResourcePanel-Constructor中添加IModel作为参数。

`public class ImageResourcesPanel扩展了Panel {

/**
 * The image names for which dynamic images will be generated
 */
private static final String[] IMAGE_NAMES = new String[] {"one", "two", "three"};

public ImageResourcesPanel(final String wicketId, IModel<Book> book) {
    super(wicketId, book);

     int refNumber = book.getModelObject().getRefNumber();


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

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

            String imageName = item.getModelObject();
            imageParameters.set("name", imageName);
            imageParameters.set("ref_number", refNumber);

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

            // 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);
}

}`