java - 如何在 Spring 中缩小动态 HTML 响应?

标签 java html spring spring-boot minify

按照 Google 的 pagespeed 建议,我希望缩小我的 Spring 应用程序的 HTML 响应。我不是指 GZip,我指的是在 HTML 发送之前从 HTML 中删除注释和空格。

我想动态而不是在我的模板中执行此操作。我的模板包含许多有用的评论,但不应成为回复的一部分。

下面是我的 Controller ;

@Controller
public class IndexController {

    @GetMapping("/")
    public ModelAndView index() {
        Data data = ....
        return new ModelAndView("index", data);
    }
}

最佳答案

我设法通过添加 javax.servlet.Filter 来做到这一点正在使用 com.googlecode.htmlcompressor 的组件进入 Spring

首先是过滤器;

@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>

关于java - 如何在 Spring 中缩小动态 HTML 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423560/

相关文章:

html - ionic 2/3/4 : How to align button in the header to the right side of header

javascript - 当访问我网站的特定 url 时,如何自动最大化浏览器窗口?

spring - 创建 session 无状态使用

java - Hibernate 映射有时会停止工作

java - 使用 DBUnit/Junit 测试我的存储库层,我应该手动构建方案还是自动化它?有什么优点?

java - 使用 Spring Security Adapter 时的 Keycloak session 超时行为

java - 在 Eclipse 或 Intellij 中取消封装重构?

java - 参数类型后面的 `...` 有什么意义?

java - 如何创建检查以防止用户添加现有值

javascript - HTML:根据值更改背景行