欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springmvc異常映射2種實(shí)現(xiàn)方法

 更新時(shí)間:2020年05月06日 11:59:24   作者:第十八使徒  
這篇文章主要介紹了Springmvc異常映射2種實(shí)現(xiàn)方法以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。,需要的朋友可以參考下

請(qǐng)求出現(xiàn) 想要跳轉(zhuǎn)到錯(cuò)誤頁面

就需要對(duì)springmvc進(jìn)行配置

方法1:基于xml的配置

springmvc.xml配置類

<!--配置基于xml的異常映射-->
<bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
  <!--配置異常和對(duì)應(yīng)頁面的映射-->
  <property name="exceptionMappings" >
    <props>
      <prop key="java.lang.Exception">erroe</prop>
    </props>
  </property>
</bean>

2.方法2:基于@ControllerAdvice

@ControllerAdvice
public class ExceptionResolver {
  @ExceptionHandler(value = NullPointerException.class)
public ModelAndView nullPointerExceptionResovler(NullPointerException e, HttpServletRequest request, HttpServletResponse response) throws IOException {
   String viewName="erroe";
    return commonReslover(viewName,response,request,e);
  }
  private ModelAndView commonReslover(String viewName,HttpServletResponse response,HttpServletRequest request,Exception e) throws IOException {
    boolean judgeResult = CrowdUtil.judgeRequestType(request);
    if(judgeResult){
      ResultEntity<Object> resultEntity=ResultEntity.failed(e.getMessage());
      //轉(zhuǎn)成gson對(duì)象
      Gson gson=new Gson();
      response.getWriter().write(gson.toJson(resultEntity));
      return null;
    }
    ModelAndView modelAndView=new ModelAndView();
    modelAndView.addObject("exception",e);
    modelAndView.setViewName(viewName);
    return modelAndView;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論