【发布时间】:2020-04-03 11:27:15
【问题描述】:
我使用过 Spring MVC 异常处理。 修改 web.xml 之类的
<error-page>
<error-code>404</error-code>
<location>/error</location>
</error-page>
@Controller
class GeneralErrorHandlerController {
@RequestMapping("error")
public String handelError(final HttpServletRequest request, final Model model) {
/**
* Process the error details received in the request
*/
// Lets get the status code and uri from the request
final Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
if (requestUri == null) {
requestUri = "Unknown";
}
// create a message to be sent back via the model object.
final String message = MessageFormat.format("{0} returned for {1}",
statusCode, requestUri);
model.addAttribute("errorMessage", message);
return "Common/Error/globalError";
}
}
但详细信息主题不适用于返回视图。所以请告诉我如何实现它。
【问题讨论】:
-
您必须按照与其他页面完全相同的方式设计错误页面。如果您这样做,您的主题将被应用。也许您可以发布更多信息?
-
我的问题是 Spring 和 SiteMesh 错误页面未装饰(跳过主过滤器)。你可以参考同样的问题,比如stackoverflow.com/questions/43593607/…
-
显然你没有捕捉到 http 错误。在我的项目中,我实现了一个抽象基类,它捕获所有 http 错误并重定向到标准化的错误页面。下方谢赫回答
-
无法获取。我已经按照 url baeldung.com/custom-error-page-spring-mvc 中定义的步骤进行操作 .. 我的问题是 detailstheme 没有应用于错误页面。
标签: java spring-mvc