2017-07-28 50 views
0

我正在研究springmvc和angular应用程序。当发生某种异常时,我需要转发到错误页面,但下面的方案不会转发到错误页面,而是转到页面,但页面是空白的,并且在检查浏览器控制台时,下面是例外情况:发生任何异常时无法转发到错误页面

Failed to load resource: the server responded with a status of 404() :8080/undefined 

而不是返回到页面并显示空白页,我想转发它到错误页面。 我在我的异常中有一个异常处理程序类,发生任何异常是处理程序类被调用,并处理并转发到错误页面。

JS代码:

myApp.controller('myController', function ($rootScope, $scope, $sce, MyService, $window) { 
    $scope.getData = function() { 
     $scope.pdfName = {}; 
     $scope.html = ''; 
     MyService.getMyData($scope.id1).then(
      function (response) { 
       $scope.myResponse = response; 
       //check for error 
       if ($scope.myResponse .error) { 
        $rootScope.showError(500, $scope.myResponse .error); 
       } else { 
       if(window.navigator.msSaveOrOpenBlob){ 
        $scope.IEBrowser = true; 
        //logic here 
        } else { 
        $scope.IEBrowser = false; 
        //some logic here} 
      }}, 

     function (errResponse) { 
      alert("in error ds"); 
      $rootScope.showError(500, errResponse); 
      $scope.pdfName = {}; 
     }); 
    } 
    $scope.getMyData(); 
}); 

//异常处理程序类

@ControllerAdvice 
public class MyExceptionControllerAdvice { 

    @ExceptionHandler(Exception.class) 
    public @ResponseBody ErrorInfoBean exception(Exception e) { 
     System.out.println("In ExceptionControllerAdvice!---- " +e.getMessage()); 
     ErrorInfoBean error = new ErrorInfoBean(); 
     error.setStatus(500); 
     error.setError(e.getMessage()); 
     return error; 
    } 
} 

从我的春天控制器时会发生的任何异常被击中以上MyExceptionControllerAdvice类,但不返回错误页面。 我试图通过在我上面的MyExceptionControllerAdvice中将500更改为404,但它没有奏效。

的web.xml: 我已经配置如下:

<error-page> 
     <error-code>500</error-code> 
     <location>/views/error.jsp</location> 
    </error-page> 
    <error-page> 
     <exception-type>404</exception-type> 
     <location>/views/error.jsp</location> 
    </error-page> 

回答

0

404错误的发生是因为坏找不到网址页面或。你正在使用spring控制器和rest服务,所以你需要使用控制器和方法的请求映射。您需要在课程和方法之前使用@RequestMapping注释。

@RequestMapping("/myExceptionClass") 
public class 

@RequestMapping(value = "/exceptio", method = RequestMethod.POST, produces = "application/json", consumes = "application/json") 
public RestResponse save(){ 

} 

所以网址是:本地主机:8080 /项目名称/ myExceptionClass /异常

+0

是的但在这里我的问题是不同的。如果我需要处理404,我如何将它重定向到错误页面,而不是在控制台中显示错误并显示空白页面。我想显示error.jsp页面,正如我在web.xml中提到的(在我上面的帖子中提到的) – DIM

0

可能是你可以使用HTTP拦截处理。

$httpProvider.interceptors.push(function($q, $cookies) { 
     return { 
     responseError: function(rejection) { 
      if (rejection.status === 404) { 
      window.location.href = '/PageNotFound'; 
      } 
      return $q.reject(rejection); 
     } 
     }; 
    }); 
+0

10我需要在哪里包含拦截器代码(在控制器或服务调用中) – DIM

+1

请参阅此链接http:/ /www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/ – Krish

相关问题