我有一个工作的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