سؤال

How could I use PageParameters on a Wicketpanel? I'm willing to load images from filesystem on a WicketPanel, and I found a tutorial for that, but they are using a Page, and in my case, I want to mount the images on a Panel. What should I change in this class or do I HAVE to implement a PageClass for this usecase? 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);
    }

}

Thanks

هل كانت مفيدة؟

المحلول 3

Got it! I Had just to add the IModel as argument in the ImageResourcePanel-Constructor.

`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, 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);
}

}`

نصائح أخرى

The example code shown does not actually use the pageParameters from the WebPage in the image handling at all, but has an additional PageParameters imageParameters field for the images. There's no reason you can't do the same in a Panel.

Something along the lines of

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

}

should work just as well as the page version shown.

I'm not sure final fields for imageParameters and imageResourceReference are appropriate though. I would probably just make them local variables within the populateItem(ListItem<String> item) method.

Update based on comments:

It appears this example produces links to images and what you want is embedded images. A better starting point for that might be the images example in the wicket-library examples page. The ImageResourceReference code from this example might however still be useful in conjunction with the other example.

Can't you just hand over the page parametersfrom the Panel/Page you embed it in? Alternatively you can use

RequestCycle.get().getPageParameters()

but I think handing it over from the embedding component would be cleaner.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top