遵循 Google 的 pagespeed 建议,我希望缩小 Spring 应用程序的 HTML 响应。我指的不是 GZip,而是在发送 HTML 之前从 HTML 中删除注释和空格。
我想动态地执行此操作,而不是在我的模板中执行此操作。我的模板包含许多有用的评论,但不应成为响应的一部分。
以下是我的控制器;
@Controller
public class IndexController {
@GetMapping("/")
public ModelAndView index() {
Data data = ....
return new ModelAndView("index", data);
}
}
我设法通过添加一个来做到这一点javax.servlet.Filter http://www.java2s.com/Tutorial/Java/0400__Servlet/Filterthatusesaresponsewrappertoconvertalloutputtouppercase.htm正在使用的组件com.googlecode.htmlcompressor https://mvnrepository.com/artifact/com.googlecode.htmlcompressor/htmlcompressor/1.5.2进入春天
首先是Filter
;
@Component
public class HtmlFilter implements Filter {
protected FilterConfig config;
public void init(FilterConfig config) throws ServletException {
this.config = config;
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws ServletException, IOException {
ServletResponse newResponse = response;
if (request instanceof HttpServletRequest) {
newResponse = new CharResponseWrapper((HttpServletResponse) response);
}
chain.doFilter(request, newResponse);
if (newResponse instanceof CharResponseWrapper) {
String text = newResponse.toString();
if (text != null) {
HtmlCompressor htmlCompressor = new HtmlCompressor();
response.getWriter().write(htmlCompressor.compress(text));
}
}
}
}
和相关的CharResponseWrapper
;
class CharResponseWrapper extends HttpServletResponseWrapper {
protected CharArrayWriter charWriter;
protected PrintWriter writer;
protected boolean getOutputStreamCalled;
protected boolean getWriterCalled;
public CharResponseWrapper(HttpServletResponse response) {
super(response);
charWriter = new CharArrayWriter();
}
public ServletOutputStream getOutputStream() throws IOException {
if (getWriterCalled) {
throw new IllegalStateException("getWriter already called");
}
getOutputStreamCalled = true;
return super.getOutputStream();
}
public PrintWriter getWriter() throws IOException {
if (writer != null) {
return writer;
}
if (getOutputStreamCalled) {
throw new IllegalStateException("getOutputStream already called");
}
getWriterCalled = true;
writer = new PrintWriter(charWriter);
return writer;
}
public String toString() {
String s = null;
if (writer != null) {
s = charWriter.toString();
}
return s;
}
}
效果非常好。转换成这么难看的html;
<!DOCTYPE HTML>
<html>
<head>
<title>
A Simple
<!-- Test-->
HTML Document
<!-- Test-->
</title>
</head>
<body>
<p>This is a very simple HTML document</p>
<!-- Test-->
<p>It only has two<!-- Test--> paragraphs</p>
<!-- Test-->
</body>
</html>
进入这个;
<!DOCTYPE HTML> <html> <head> <title> A Simple HTML Document </title> </head> <body> <p>This is a very simple HTML document</p> <p>It only has two paragraphs</p> </body> </html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)