目前有登录模块,订单模块,还有portal主界面模块,每个模块下都有自己的controller以及web页面.页面模版用的是freemarker.
我加了一个common模块,里面有top.ftl,head.ftl,footer.ftl,这些是让其他页面include的.404.ftl,500.ftl也在里面.
我希望不同模块的web页面出现404,500之类的错误,都可以跳转到common模块的404.ftl,500.ftl,
但是目前只能是每个模块都加上这段代码跳转/404,以及每个模块都加上controller,处理/404
@Configuration
public class ErrorConfiguration {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer()
{
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
Set<ErrorPage> errorPages=new HashSet<>();
errorPages.add(new ErrorPage(HttpStatus.BAD_REQUEST,"/404"));
errorPages.add(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/500"));
errorPages.add(new ErrorPage(HttpStatus.NOT_FOUND,"/404"));
configurableEmbeddedServletContainer.setErrorPages(errorPages);
}
};
}
}``````
@RequestMapping({ "/404" })
public void error404(HttpServletRequest request,
HttpServletResponse response) {
request.getRequestDispatcher("http://localhost:8085/404");
}newErrorPage只能加/开头的url,不能加http://locahost这种.
这样每个模块都有相同的几乎两个文件,有没有更好的能减少重复重复代码的方法?
如果代码结构有问题的话也烦请指点一下.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2310/viewspace-3139033/ ,如需转载,请注明出处,否则将追究法律责任。