一、开发环境:springboot1.5.13,
二、普通ssm框架配置errorPage:在普通的web项目中,自定义errorPage方式为:在web.xml中添加如下配配置,location表示自定义jsp页面的存放路径,error-code表示错误状态,这样就完成了自定义errorPage的设置
<error-page>
<error-code>403</error-code>
<location>/WEB-INF/views/common/error_403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/common/error_404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/views/common/error_500.jsp</location>
</error-page>
三、在springboot项目中配置errorPage:springboot项目中自定义errorPage页面比普通的ssm项目会稍微难一点方法也很多,这里写两种,请注意这两种方法的区别:方法一仅适用于内嵌tomcat(重点:打war包后仍在tomcat里跑是不可行的!!!),方法二可以直接打war包,然后扔到tomcat里跑(本人因为使用了方法一,并且把war包扔进了tomcat,导致后来出现bug并重新修改,所以这里一定要注意!)
方法一:写一个配置类,将自定义的errorPage对应的路径添加进去就OK了,非常简单,具体代码如下:
@Configuration
public class ErrorPagesConfig {
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return new EmbeddedServletContainerCustomizer() {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
//状态码 :HttpStatus.NOT_FOUND(404) 错误页面的存储路径:/WEB-INF/views/common/error_404.jsp
ErrorPage errorPage400 = new ErrorPage(HttpStatus.NOT_FOUND, "/WEB-INF/views/common/error_404.jsp");
ErrorPage errorPage404 = new ErrorPage(HttpStatus.FORBIDDEN, "/WEB-INF/views/common/error_403.jsp");
ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/WEB-INF/views/common/error_500.jsp");
container.addErrorPages(errorPage400, errorPage404, errorPage500);
}
};
}
}
然后就没了,就这么简单,可是你打了war包扔进服务器之后会发现根本不起作用(尴尬),所以这里需要使用第二种方法;
方法二:使用拦截器进行拦截,拦截到后重定向到一个ErrorPageController,通过controller跳转到自定义的errorPage页面,具体实现如下:
1、自定义一个拦截器,继承HandlerInterceptorAdapter,捕获响应状态,当响应存在403/404/500的时候重定向到/error/{errorCode}对应的controller
package com.ccx.axis.core.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
/**
* @program: ccx-axis-change
* @description: 自定义errorPage拦截器
* @author: YangPeng
* @create: 2018-07-11 15:51
**/
@Component
public class ErrorPageInterceptor extends HandlerInterceptorAdapter {
private List<Integer> errorCodeList = Arrays.asList(404, 403, 500);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws
Exception {
if (errorCodeList.contains(response.getStatus())) {
//捕获异常后进行重定向,controller对应的requestMapping为/error/{code}
response.sendRedirect("/error/" + response.getStatus());
return false;
}
return super.preHandle(request, response, handler);
}
}
2、写一个controller,用来接收拦截器捕获后的异常
package com.ccx.axis.controller;
import com.ccx.axis.core.annotations.IgnoreSecurity;
import com.ccx.axis.core.tokens.JsonWebToken;
import com.ccx.axis.service.CustClient;
import com.ccx.axis.service.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import static com.ccx.axis.core.tokens.JsonWebToken.createToken;
/**
* @program: ccx-axis-change
* @description: 自定义controller,来接收拦截器拦截后重定向的请求
* @author: YangPeng
* @create: 2018-07-11 15:51
**/
@Controller
public class ErrorPageController {
@RequestMapping(value = "/error/{code}")
public String error(@PathVariable int code, Model model) {
String pager = "";
switch (code) {
case 403:
model.addAttribute("code", 404);
pager = "/common/error_403";
break;
case 404:
model.addAttribute("code", 500);
pager = "/common/error_404";
break;
case 500:
model.addAttribute("code", 500);
pager = "/common/error_500";
break;
}
return pager;
}
}
3、将自定义的拦截器添加到拦截器链中
package com.ccx.axis.config;
import com.ccx.axis.core.interceptor.AuthInterceptor;
import com.ccx.axis.core.interceptor.ErrorPageInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ErrorPageInterceptor());
super.addInterceptors(registry);
}
然后重启系统,打包、发布、访问:
perfect!