2014-10-28 3 views
0

내 Wicket 페이지 (버전 6.x)를 PortletContainer (실제로는 약간의 제한이 있음)으로 렌더링합니다.전체 Wicket Header를 본체에 표시합니다.

내 html 마크 업에 html이없고 head 태그가 있기 때문에 그 중 하나는 아약스를 사용할 수 없다는 것입니다. 렌더링하는 내용은 body 태그의 내용 일뿐입니다.

마크 업에 head 태그가있는 한 작동하는 HeaderResponseContainer을 사용하려고했습니다. IHeaderResponseDecorator이없는 경우 RequestCycle에 설정되지 않습니다.

head 태그가없는 경우에도 body에있는 일부 컨테이너에 head 태그로 렌더링 될 내용을 모두 렌더링하는 가장 좋은 방법은 무엇인지 모르겠습니다.

어떤 아이디어로이 문제를 해결할 수 있습니까?

답변

0

해킹처럼 보이지만 작동합니다. 아무도 깨끗한 솔루션을 제시 할 수없는 경우 여기에 게시하십시오.

그렇지 않으면이 비슷한 경우에 도움이 될 수 있습니다 :

당신의 Application 클래스에서 : MyWebResponse의

@Override 
protected WebResponse newWebResponse(WebRequest webRequest, 
     HttpServletResponse httpServletResponse) { 
    return new MyWebResponse((ServletWebRequest) webRequest, 
      httpServletResponse); 
} 

구현 :

public class MyWebResponse extends ServletWebResponse { 
    static Pattern bodyContentPattern = Pattern.compile("<body[^>]*>((?s).*)</body>"); 
    static Pattern headContentPattern = Pattern.compile("<head[^>]*>((?s).*)</head>"); 

    public MyWebResponse(ServletWebRequest webRequest, 
      HttpServletResponse httpServletResponse) { 
     super(webRequest, httpServletResponse); 
    } 

    @Override 
    public void write(CharSequence sequence) { 
     String string = sequence.toString(); 
     // if there is a html tag, take the body content, append the header content in 
     // a div tag and write it to the output stream 
     if (string.contains("</html>")) { 
      StringBuilder sb = new StringBuilder(); 
      sb.append(findContent(string, bodyContentPattern)); 

      String headContent = findContent(string, headContentPattern); 
      if (headContent != null && headContent.length() > 0) { 
       sb.append("<div class=\"head\">"); 
       sb.append(headContent); 
       sb.append("</div>"); 
      } 
      super.write(sb.toString()); 
     } else { 
      super.write(sequence); 
     } 
    } 

    private String findContent(String sequence, Pattern p) { 
     Matcher m = p.matcher(sequence); 
     if (m.find()) { 
      return m.group(1); 
     } 
     return sequence; 
    } 
}