我有一個工作的AngularJS + Spring MVC應用程序叫做A.這個web應用程序已被克隆(ctrl + c & ctrl + v在項目文件夾中) B.Spring MVC - Servlet調度器拋出異常:java.lang.StackOverflowError
試圖運行它們(自然地)具有相同的行爲。
我的目標是簡化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
一個級別的所有資源。
下一步就是要適應在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");
}
}
angular.ui
路由器內app.js
文件:
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都沒有什麼經驗,我無法弄清楚我錯過了什麼。
嘗試改變sth.html的名稱別的東西和唐忘記改變它也在控制器中返回新的ModelAndView(「sth」); – reos
其實'sth'是我在這個線程中使用的通用佔位符,但實際上它的名字是'grid',因爲那個頁面顯示了一個表格。 – grimi
我問你這是因爲你的@RequestMapping(value =「/ sth」) public ModelAndView getGrid(){ return new ModelAndView(「sth」); }我想這可能會導致無窮無盡。 – reos