Spring WebApplicationInitializer和Jetty 8.x.

时间:2012-02-06 21:30:48

标签: spring maven spring-mvc jetty

我正在尝试使用Maven在Jetty中部署应用程序:

我的插件配置如下:

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>8.0.4.v20111024</version>
</plugin>

我有一个WebApplicationContextInitializer,我把它简化为一个简单的形式:

AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();

// Register and map the main dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("appServlet", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(2);
dispatcher.addMapping("/site/*");

当我运行jetty时:从Spring Tool Suite中运行我无法访问我的servlet。启动日志是:

    [INFO] Configuring Jetty for project: Test
    [INFO] webAppSourceDirectory C:\Users\Alex\Documents\spring\Test\src\main\webapp does not exist. Defaulting to C:\Users\Alex\Documents\spring\Test\src\main\webapp
    [INFO] Reload Mechanic: automatic
    [INFO] Classes = C:\Users\Alex\Documents\spring\Test\target\classes
    [INFO] Context path = /
    [INFO] Tmp directory = C:\Users\Alex\Documents\spring\Test\target\tmp
    [INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
    [INFO] Web overrides =  none
    [INFO] web.xml file = null
    [INFO] Webapp directory = C:\Users\Alex\Documents\spring\Test\src\main\webapp
    2012-02-06 21:22:38.048:INFO:oejs.Server:jetty-8.0.4.v20111024
    2012-02-06 21:22:38.807:INFO:oejpw.PlusConfiguration:No Transaction manager found - if your webapp requires one, please configure one.
    2012-02-06 21:22:41.828:INFO:/:Spring WebApplicationInitializers detected on classpath: [org.test.application.config.TestWebApplicationInitializer@f946f9]
    2012-02-06 21:22:41.965:INFO:/:Initializing Spring FrameworkServlet 'appServlet'
    INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
    INFO : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Mon Feb 06 21:22:41 GMT 2012]; root of context hierarchy
    INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
    INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@18b24cb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
    INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 235 ms
    2012-02-06 21:22:42.344:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/},file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/
    2012-02-06 21:22:42.344:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/},file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/
    2012-02-06 21:22:42.345:INFO:oejsh.ContextHandler:started o.m.j.p.JettyWebAppContext{/,file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/},file:/C:/Users/Alex/Documents/spring/Test/src/main/webapp/
    2012-02-06 2012-02-06 21:22:42.352:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080 STARTING
    [INFO] Started Jetty Server

如果然后导航到http://localhost:8080/,我会得到默认的Jetty页面,但没有列出上下文。

我已经尝试将webdefaults.xml复制到我的项目中here,但这并没有解决问题。它只是删除了默认的servlet页面。

这在Tomcat中正确部署,所以我怀疑maven-jetty-plugin中存在问题。

有没有人有这方面的经验?

编辑:

所以我可以确认,如果我更改了Jetty配置,以便在上下文/应用程序下部署应用程序,然后导航到http://localhost:8080/application/site/,我会得到404。

但是,调度程序servlet已记录:

No mapping found for HTTP request with URI [/application/site/] in DispatcherServlet with name 'appServlet'

这表明我的控制器映射存在问题吗?

启动日志显示此映射已注册:

Mapped "{[/],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String org.test.application.controller.HomeController.catchAll()

我错过了什么?

1 个答案:

答案 0 :(得分:0)

似乎新的Jetty插件使用root“/”作为Web应用程序的根上下文,旧的如下所示:

  

contextPath可选。您的webapp的上下文路径。默认情况下,   这是从项目的pom.xml设置的。您可以   覆盖它并将其设置为您喜欢的任何内容。