【问题标题】:unable to forward to the error page when any exception is occured发生异常时无法转发到错误页面
【发布时间】:2017-07-28 04:24:01
【问题描述】:

我正在研究 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;
    }
}

从我的 spring 控制器中,当发生任何异常时,它会点击上面的 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>

【问题讨论】:

    标签: javascript java angularjs spring


    【解决方案1】:

    404 错误是因为找不到页面或错误的 url。您正在使用弹簧控制器和休息服务,因此您需要使用控制器和方法的请求映射。你需要在类和方法之前使用@RequestMapping注解。

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

    所以 url 将是:localhost:8080/projectname/myExceptionClass/exception

    【讨论】:

    • 是的,但我的问题有所不同。如果我需要处理 404,如何将其重定向到错误页面,而不是在控制台中显示错误并显示空白页面。我想显示我在 web.xml 中提到的 error.jsp 页面(在我上面的帖子中提到)
    【解决方案2】:

    也许你可以使用 http 拦截器来处理它。

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

    【讨论】:

    猜你喜欢
    • 2017-04-05
    • 2015-01-04
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2018-11-20
    • 2014-09-14
    相关资源
    最近更新 更多