You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by "Edvard Fonsell (JIRA)" <ji...@apache.org> on 2013/06/13 08:50:20 UTC

[jira] [Commented] (WICKET-5232) ComponentRenderer.renderComponent could accept components with any markupId, not just "compId"

    [ https://issues.apache.org/jira/browse/WICKET-5232?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13681961#comment-13681961 ] 

Edvard Fonsell commented on WICKET-5232:
----------------------------------------

I was able to make it work for me with these modifications to ComponentRenderer:

1. Store the original parent of the component before rendering
2. Pass the component ID to the RenderPage constructor
3. Add the component back to the original parent after rendering (otherwise the component tree of the original page will be broken)
4. Use the given component ID in RenderPage to generate the markup
5. Disable caching of the RenderPage markup (otherwise next call to renderComponent with a different component ID will fail)

    public static CharSequence renderComponent(final Component component) {
        RequestCycle requestCycle = RequestCycle.get();
        final Response originalResponse = requestCycle.getResponse();
        final MarkupContainer originalMarkupContainer = component.getParent();
        BufferedWebResponse tempResponse = new BufferedWebResponse(null);
        try {
            requestCycle.setResponse(tempResponse);
            RenderPage page = new RenderPage(component.getId());
            page.add(component);
            component.render();
        } finally {
            requestCycle.setResponse(originalResponse);
            originalMarkupContainer.add(component);
        }
        return tempResponse.getText();
    }

    private static class RenderPage extends WebPage implements IMarkupResourceStreamProvider, IMarkupCacheKeyProvider {
        private static final long serialVersionUID = 1L;
        private static final String MARKUP = "<wicket:container wicket:id=\"%s\"></wicket:container>";
        private final String componentId;

        public RenderPage(final String componentId) {
            this.componentId = componentId;
        }

        @Override
        public IResourceStream getMarkupResourceStream(final MarkupContainer container, final Class<?> containerClass) {
            return new StringResourceStream(String.format(MARKUP, componentId));
        }

        @Override
        public String getCacheKey(final MarkupContainer container, final Class<?> containerClass) {
            return null;
        }
    }

                
> ComponentRenderer.renderComponent could accept components with any markupId, not just "compId"
> ----------------------------------------------------------------------------------------------
>
>                 Key: WICKET-5232
>                 URL: https://issues.apache.org/jira/browse/WICKET-5232
>             Project: Wicket
>          Issue Type: Improvement
>          Components: wicket
>    Affects Versions: 6.8.0
>            Reporter: Edvard Fonsell
>            Priority: Minor
>
> If you pass an existing component that has an arbitrary markupId to ComponentRenderer.renderComponent, it will fail:
> org.apache.wicket.markup.MarkupNotFoundException: Markup not found for Component: [Component id = myComponentId]
> This is because RenderPage markup uses a constant COMP_ID for the markupId:
> private static final String MARKUP = "<wicket:container wicket:id='" + COMP_ID + "'></wicket:container>";
> It would be nice if the ComponentRenderer would use the markupId of the given component.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira