title: Wicket 1.5 and JSP/servlet wrapping
body: <p>In the project I'm on we want to upgrade from Wicket 1.4 to 1.5. After some work we got most things working OK.  </p>
  
  <p>However one major thing is not working yet. There's a need to wrap old JSP/servlets into the new Wicket based application and the old 1.4-approach is not working anymore.</p>
  
  <p>Simplified html output in 1.4</p>
  
  <pre><code>&lt;body&gt;
      &lt;div id="container"&gt;
          wrappedContentFromJsp
      &lt;/div&gt;  
  &lt;body&gt;
  </code></pre>
  
  <p>Simplified html output in 1.5</p>
  
  <pre><code>&lt;body&gt;
      wrappedContentFromJsp   
      &lt;div id="container"&gt;        
      &lt;/div&gt;  
  &lt;body&gt;
  </code></pre>
  
  <p>So, all the <strong>JSP content renders outside the tag that we like to wrap it in</strong>. </p>
  
  <p>The wrapping magic happens in our internal <code>AbstractServletWrapperPanel</code> and the <code>WebMarkupContainer.onRender(MarkupStream markupStream)</code> override. However, <strong>in Wicket 1.5 we can't invoke <code>markupStream.next()</code> since it's no longer provided</strong>. I have not found a way around this yet.</p>
  
  <p>Working code for 1.4 with a sample panel implementation as reference:</p>
  
  <pre><code>public abstract class AbstractServletWrapperPanel extends Panel {
  
      public AbstractServletWrapperPanel(String id, final  String servletName, String tagId) {
          super(id);
          add(new WebMarkupContainer(tagId) {
  
              @Override
              protected void onRender(MarkupStream markupStream) {
                  markupStream.next();
                  try {
                      WebRequestCycle cycle = (WebRequestCycle) RequestCycle.get();
                      ServletRequest request = cycle.getWebRequest().getHttpServletRequest();
                      ServletResponse response = cycle.getWebResponse().getHttpServletResponse();
                      ServletContext context = ((WebApplication) Application.get()).getServletContext();
                      RequestDispatcher rd = context.getNamedDispatcher(servletName);
                      if (rd != null) {
                          rd.include(request, response);
                      } else {
                          // handling...
                      }
                  } catch (Exception e) {
                      // handling...
                  }
              }
          });
      }
  }
  
  //Impl
  public class WrapperPanel extends AbstractServletWrapperPanel {
      private static final long serialVersionUID = 1L;
  
      public WrapperPanel(String id, final String servletName) {
          super(id, servletName, "wrappedContentId");
      }   
  }
  
  //WrapperPanel html
  &lt;body&gt;
      &lt;wicket:panel&gt;
          &lt;wicket:container wicket:id="wrappedContentId"/&gt;
      &lt;/wicket:panel&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>In the 1.5 version I'm getting the request and response via <code>(HttpServletRequest)RequestCycle.get().getRequest().getContainerRequest()</code> and <code>(HttpServletResponse)RequestCycle.get().getResponse().getContainerResponse()</code></p>
  
  <p>Then I've tried to:  </p>
  
  <ul>
  <li>use the onRender()-magic without <code>markupStream.next()</code> that's no longer provided in 1.5  </li>
  <li>move it to <code>onComponentTagBody(MarkupStream markupStream, ComponentTag tag)</code> 
  <ul>
  <li>Note: To invoke onComponentTagBody() I had to open up the wicket:container tag <code>&lt;wicket:container wicket:id="wrappedContentId"&gt;&lt;/wicket:container&gt;</code>.  I also tried without invoking <code>markupStream.next()</code> as that step is performed in <code>Component.internalRenderComponent()</code> just before <code>onComponentTagBody</code> is invoked at all.</li>
  </ul></li>
  <li>move it to <code>onComponentTag(ComponentTag tag)</code>  </li>
  <li>combined above with setting <code>setRenderBodyOnly(true)</code> in the <code>WebMarkupContatiner.onInitialize()</code>  </li>
  <li>use a <code>&lt;div&gt;</code> tag instead of a <code>wicket:container</code>  </li>
  <li>use debug mode to track down the rendering process of 1.5. But still, I guess I'm missing out some key part of the new 1.5 way of rendering components. </li>
  </ul>
  
  <p>Since it's not an option to migrate all that JSP functionality to Wicket anytime soon this is kind of a showstopper for us at the moment.</p>
  
  <p>For reference, the 1.4 way of doing this is much similar to the approach I found in the article <a href="http://herebebeasties.com/2007-03-01/jsp-and-wicket-sitting-in-a-tree/" rel="nofollow">jsp-and-wicket-sitting-in-a-tree</a> and on the <a href="https://cwiki.apache.org/WICKET/including-jsp-files-in-html-templates.html" rel="nofollow">Wicket wiki</a></p>
  
  <p>Any help solving this issue would be very appreciated!</p>
  
  <p>[EDIT]<br>
  After a suggestion from TheStijn I've also tried invoking <code>getAssociatedMarkupStream()</code> from <code>onRender()</code> but that raises the following error: <code>org.apache.wicket.markup.MarkupNotFoundException: Markup of type 'html' for component '... AbstractServletWrapperPanel$1' not found.</code></p>