반응형
봄에 동적 HTML 응답을 최소화하려면 어떻게 해야 합니까?
구글의 페이지스피드 조언에 따라 Spring 어플리케이션의 HTML 응답을 최소화해 주셨으면 합니다.GZip이 아니라 회선으로 전송되기 전에 HTML에서 코멘트와 여백을 삭제하는 것을 의미합니다.
템플릿이 아닌 동적으로 이 작업을 수행하고자 합니다.템플릿에는 유용한 코멘트가 많이 포함되어 있지만 응답에는 포함되지 않아야 합니다.
컨트롤러는 다음과 같습니다.
@Controller
public class IndexController {
@GetMapping("/")
public ModelAndView index() {
Data data = ....
return new ModelAndView("index", data);
}
}
스프링에 사용 중인 컴포넌트를 추가하여 이 작업을 수행할 수 있었습니다.
일단은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>
언급URL : https://stackoverflow.com/questions/46423560/how-do-i-minify-dynamic-html-responses-in-spring
반응형
'programing' 카테고리의 다른 글
| 직렬화 가능한 python 객체의 json 인코딩 동작을 변경하는 방법은 무엇입니까? (0) | 2023.03.26 |
|---|---|
| 유형에 대해 JSON.NET 오류 자체 참조 루프가 탐지되었습니다. (0) | 2023.03.26 |
| 문자 영역(material-ui) (0) | 2023.03.21 |
| Wordpress : taxonomy.php에서 분류명을 취득하려면 어떻게 해야 합니까? (0) | 2023.03.21 |
| 어떤 GUP 버전을 설치했는지 확인할 수 있는 방법이 있습니까? (0) | 2023.03.21 |