I am developing a web application using Spring MVC and SiteMesh 3. After recently upgrading the Spring Framework version to 6.2.4, I noticed that error pages generated by HandlerExceptionResolver no longer have the layout defined by SiteMesh 3 applied. In the previous version, 6.2.3, the SiteMesh 3 layout was applied correctly even when errors occurred.
Here's how to reproduce the issue:
First, define a SiteMesh filter with the following code. Here, we are simply applying one layout to all paths.
@Configuration
public class WebConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Bean
public FilterRegistrationBean<ConfigurableSiteMeshFilter> siteMeshFilter() {
FilterRegistrationBean<ConfigurableSiteMeshFilter> filter = new FilterRegistrationBean<>();
filter.setFilter(new ConfigurableSiteMeshFilter() {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
builder
.addDecoratorPath("/*", "main.jsp")
.addExcludedPath("/decorators/*")
.addExcludedPath("/WEB-INF/*");
}
});
filter.addUrlPatterns("/*");
return filter;
}
}
In a controller method, describe exception handling with @ExceptionHandler
as follows. For testing purposes, an exception is triggered when accessing /error-test
.
@Controller
@RequestMapping("/")
public class HomeController {
@GetMapping
public String home() {
return "home";
}
@GetMapping("/error-test")
public String errorTest() {
throw new RuntimeException();
}
@ExceptionHandler(RuntimeException.class)
public String handleRuntimeException(RuntimeException e) {
return "home-error";
}
}
The view page should look something like this.
home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
</head>
<body>
<h2>Home Body</h2>
</body>
</html>
home-error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h2>Error Body</h2>
</body>
</html>
Prepare a SiteMesh layout like the following. It adds a header and a footer to the main content.
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><sitemesh:write property='title'/></title>
<sitemesh:write property='head'/>
</head>
<body>
<div class="header">
<h1>SiteMesh Header</h1>
</div>
<div class="content">
<sitemesh:write property='body'/>
</div>
<div class="footer">
<p>SiteMesh Footer</p>
</div>
</body>
</html>
The expected behavior with this setup is that the SiteMesh layout is applied both when accessing /
and when accessing /error-test
. However, after upgrading to Spring Framework 6.2.4, the layout is not applied when accessing /error-test
, and only the home-error.jsp
page itself is displayed.
I noticed commit f895d76 in the Spring Framework () which involves removing the Content-Type header in error cases.
It appears that the execution of response.setHeader(HttpHeaders.CONTENT_TYPE, null)
in DispatcherServlet#processHandlerException()
causes HttpServletResponseBuffer.bufferingWasDisabled
to become true
, which seems to be why the layout is no longer being applied.
Questions
- Is it possible that the Content-Type header being set to null during error handling in Spring Framework 6.2.4 is affecting the application of SiteMesh 3 layout to error pages? Specifically, how might the state of HttpServletResponseBuffer.bufferingWasDisabled be impacted?
- If so, are there any configuration or implementation changes I can make to resolve this issue?
- Are there any known similar cases or solutions related to SiteMesh 3 error layout issues after upgrading to Spring Framework 6.2.4?
Any help or insights would be greatly appreciated.