해킹처럼 보이지만 작동합니다. 아무도 깨끗한 솔루션을 제시 할 수없는 경우 여기에 게시하십시오.
그렇지 않으면이 비슷한 경우에 도움이 될 수 있습니다 :
당신의 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;
}
}