Java SpringWebApplicationInitializer配置多个servlet
我需要配置2个servlet:一个用于常规http请求,另一个用于JavaWebSocket的Atmosphere servlet 以下是我的WebApplicationInitializer的代码:Java SpringWebApplicationInitializer配置多个servlet,java,spring,configuration,Java,Spring,Configuration,我需要配置2个servlet:一个用于常规http请求,另一个用于JavaWebSocket的Atmosphere servlet 以下是我的WebApplicationInitializer的代码: public class AppInitializer implements WebApplicationInitializer { private static final String CONFIG_LOCATION = "com.mysite.myapp.presentation.c
public class AppInitializer implements WebApplicationInitializer
{
private static final String CONFIG_LOCATION = "com.mysite.myapp.presentation.config";
private static final String MAPPING_URL = "/*";
private static final String STREAM_URL = "/stream/*";
private int servletInx = 1;
private ServletContext servletContext;
@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
this.servletContext = servletContext;
WebApplicationContext context = getContext();
servletContext.addListener(new ContextLoaderListener(context));
registerServlet("DispatcherServlet", new DispatcherServlet(context), MAPPING_URL);
registerServlet("AtmosphereServlet", new AtmosphereServlet(), STREAM_URL);
}
private AnnotationConfigWebApplicationContext getContext()
{
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation(CONFIG_LOCATION);
return context;
}
private void registerServlet(String servletName, Servlet servletClass, String mappingUrl)
{
ServletRegistration.Dynamic dispatcher =
servletContext.addServlet(servletName, servletClass);
if (dispatcher != null)
{
System.out.println("servletInx: " + servletInx);
dispatcher.setLoadOnStartup(servletInx++);
dispatcher.addMapping(mappingUrl);
}
}
}
运行应用程序时,http部分工作正常;但是,没有提供任何静态文件。甚至webapps/myapp(localhost:8080/myapp/index.html)上的index.html也返回404
当控制器返回相同的html时
通过/@RequestMapping(value=“/welcome”,method=RequestMethod.GET),
它可以工作,但html中指定的任何javascript或css都返回404
任何帮助都将不胜感激抱歉,这与WebApplicationInitializer无关。这与WebMVCConfigureAdapter有关。缺少启用配置器的configureDefaultServletHandling方法。这反过来又使应用程序能够提供静态内容 很抱歉,我的头被刮伤了;-)