簡體   English   中英

Spring MVC-Servlet調度程序拋出異常:java.lang.StackOverflowError

[英]Spring MVC - Servlet dispatcher threw exception: java.lang.StackOverflowError

我有一個工作正常的AngularJS + Spring MVC應用程序,稱為A。此Web應用程序已被克隆(項目文件夾上的ctrl + c和ctrl + v)到另一個名為B的應用程序中。

嘗試同時運行它們(自然)具有相同的行為。

我的目標是簡化A,但在第一步中,我想做的就是對包含.html文件的文件夾進行一些重構。

樹形文件夾如下:

src/main/webapp
           |--- WEB-INF/
           |--- META-INF/
           |--- static/
                   |--- js/
                   |--- css/
                   |--- index.html
                   |--- sth.html

B上的轉換為:

src/main/webapp
           |--- META-INF/
           |--- js/
           |--- css/
           |--- index.html
           |--- sth.html

簡要地說,我將static文件夾中的所有資源上移了一層。

下一步是從舊WebMvcConfigurerAdapter開始改編A WebMvcConfigurerAdapter (是的,一旦解決,我將做一個更好的組織代碼... ARG !!!):

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "my.package.name")
public class Configurations extends WebMvcConfigurerAdapter {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);

        viewResolver.setPrefix("/static/");
        viewResolver.setSuffix(".html");

        registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //resources locations

        // html
        registry.addResourceHandler("static/**").addResourceLocations("/static/");
        registry.addResourceHandler("static/modals/**").addResourceLocations("/static/modals/");

        // css
        registry.addResourceHandler("css/**").addResourceLocations("/static/css/");
        registry.addResourceHandler("css/bootstrap/**").addResourceLocations("/static/css/bootstrap/");
        registry.addResourceHandler("js/fullcalendar/dist/**").addResourceLocations("/static/js/fullcalendar/dist/");

        // scripts
        registry.addResourceHandler("js/**").addResourceLocations("/static/js/");
        registry.addResourceHandler("js/jquery/**").addResourceLocations("/static/js/jquery/");
        registry.addResourceHandler("js/bootstrap/**").addResourceLocations("/static/js/bootstrap/");
        registry.addResourceHandler("js/bootstrap/ui-bootstrap/**").addResourceLocations("/static/js/bootstrap/ui-bootstrap/");
        registry.addResourceHandler("js/bootstrap/umd/**").addResourceLocations("/static/js/bootstrap/umd/");
        registry.addResourceHandler("js/angular/**").addResourceLocations("/static/js/angular/");
        registry.addResourceHandler("js/angular/animate/**").addResourceLocations("/static/js/angular/animate/");
        registry.addResourceHandler("js/angular/ui-router/release/**").addResourceLocations("/static/js/angular/ui-router/release/");
        registry.addResourceHandler("js/angular-ui-calendar/src/**").addResourceLocations("/static/js/angular-ui-calendar/src/");
        registry.addResourceHandler("js/fullcalendar/dist/**").addResourceLocations("/static/js/fullcalendar/dist/");
        registry.addResourceHandler("js/moment/**").addResourceLocations("/static/js/moment/");
        registry.addResourceHandler("js/angular-ui-grid/**").addResourceLocations("/static/js/angular-ui-grid/");

        // custom
        registry.addResourceHandler("js/service/**").addResourceLocations("/static/js/service/");
        registry.addResourceHandler("js/controller/**").addResourceLocations("/static/js/controller/");
        registry.addResourceHandler("js/controller/modals/**").addResourceLocations("/static/js/controller/modals/");
    }

}

換一個新的B:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "my.package.name")
public class Configurations extends WebMvcConfigurerAdapter {

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);

        viewResolver.setPrefix("/");
        viewResolver.setSuffix(".html");

        registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //resources locations

        // html
        registry.addResourceHandler("/**").addResourceLocations("/");

        // css
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        registry.addResourceHandler("/css/bootstrap/**").addResourceLocations("/css/bootstrap/");
        registry.addResourceHandler("/js/fullcalendar/dist/**").addResourceLocations("/js/fullcalendar/dist/");

        // scripts
        registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        registry.addResourceHandler("/js/jquery/**").addResourceLocations("/js/jquery/");
        registry.addResourceHandler("/js/bootstrap/**").addResourceLocations("/js/bootstrap/");
        registry.addResourceHandler("/js/bootstrap/ui-bootstrap/**").addResourceLocations("/js/bootstrap/ui-bootstrap/");
        registry.addResourceHandler("/js/bootstrap/umd/**").addResourceLocations("/js/bootstrap/umd/");
        registry.addResourceHandler("/js/angular/**").addResourceLocations("/js/angular/");
        registry.addResourceHandler("/js/angular/animate/**").addResourceLocations("/js/angular/animate/");
        registry.addResourceHandler("/js/angular/ui-router/release/**").addResourceLocations("/js/angular/ui-router/release/");
        registry.addResourceHandler("/js/angular-ui-calendar/src/**").addResourceLocations("/js/angular-ui-calendar/src/");
        registry.addResourceHandler("/js/fullcalendar/dist/**").addResourceLocations("/js/fullcalendar/dist/");
        registry.addResourceHandler("/js/moment/**").addResourceLocations("/js/moment/");
        registry.addResourceHandler("/js/angular-ui-grid/**").addResourceLocations("/js/angular-ui-grid/");

        // custom
        registry.addResourceHandler("/js/service/**").addResourceLocations("/js/service/");
        registry.addResourceHandler("/js/controller/**").addResourceLocations("/js/controller/");
    }

}

如圖所示,我在每個路徑上刪除了static/前綴。

一旦運行,遞歸瀏覽器的控制台給我Error 500請求sth盡管成功地給我index.html

GET http://localhost:8080/MyWebApp/sth 500 (Internal Server Error)

當Tomcat長時間記錄此異常時:

    SEVERE: Servlet.service() for servlet dispatcher threw exception
    java.lang.StackOverflowError
        at javax.servlet.ServletRequestWrapper.getRemoteAddr(ServletRequestWrapper.java:221)
        at javax.servlet.ServletRequestWrapper.getRemoteAddr(ServletRequestWrapper.java:221)
        at javax.servlet.ServletRequestWrapper.getRemoteAddr(ServletRequestWrapper.java:221)
// lots of times...

    at org.springframework.web.servlet.FrameworkServlet.publishRequestHandledEvent(FrameworkServlet.java:1075)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
        at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:168)
        at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1244)
        at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)

//To the infinity and beyond! (cit.)... unless tomcat shutdown

這是索引控制器:

@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getIndexPage() {
        return new ModelAndView("index");
    }

}

該控制器來管理sth

@Controller
public class TemplateController {

    @RequestMapping(value = "/sth")
    public ModelAndView getGrid() {
        return new ModelAndView("sth");
    }

}

app.js文件中的angular.ui路由器:

var app = angular.module('MyWebApp', [
    'ui.router',
    'ui.calendar',
    'ngAnimate',
    'ui.bootstrap',
    'ui.grid'
]);

//globals
app.value('baseUrl', '/MyWebApp');

app.config(function ($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise('/sth');

    $stateProvider.state('sth', {
        url: '/sth',
        templateUrl: 'sth',
        controller: 'sthCtrl',
        controllerAs: 'sth'
    });

});

最后是sthCtrl文件

app.controller('sthCtrl', function ($http, baseUrl) {
    var scope = this;
    scope.users = [];
    scope.structures = [];

    $http.get(baseUrl + '/users').then(function (userResp) {
        scope.users = userResp.data;
    });

    $http.get(baseUrl + '/structures').then(function (structureResp) {
        scope.structures = structureResp.data;
    });

});

我必須錯過了一些我看不見的東西...但是,我有一些懷疑:

1)在“ Configurations課程中:我可以忘記一些東西嗎? 2)這是一個前端結構性問題:“自動”創建static文件夾並將所有先前的人群移入內部(也更改了Configurations類的路徑) Error 500消失了,我可以看到我的HTML! (OMG),所以我猜想AngularJS是否需要static文件夾,並且如果我不想以某種方式告訴它...但是Tomcat上的異常仍然是(廢話...)

對AngularJS和Spring MVC缺乏經驗,我無法解決我所缺少的東西。

您需要返回一個將由ViewResolver解析為View的字符串,請嘗試執行以下操作以避免無限循環:

 @RequestMapping(value = "/sth")
  public String getGrid() {
    return "someThing";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM