【问题标题】:Jetty isn't picking up my spring applicationJetty 没有收到我的春季申请
【发布时间】:2012-08-15 23:18:40
【问题描述】:

我有一个使用休眠、freemarker 的 Spring MVC 应用程序。它被设置为一个多 Maven 项目。我正在使用 IntelliJ 终极版。

码头开始很好,但是当我去

http://localhost:8080/

它只是输出我的项目的文件夹,我可以在浏览器中查看我的源代码!

这是我目前的设置:

    final Server server = new Server(8080);

    ProtectionDomain domain = HttpServer.class.getProtectionDomain();
    URL location = domain.getCodeSource().getLocation();

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setResourceBase(location.toExternalForm());
    webAppContext.setDescriptor(location.toExternalForm() + "/WEB-INF/web.xml");
    webAppContext.setContextPath("/");
    webAppContext.setParentLoaderPriority(true);

    server.setHandler(webAppContext);

    server.start();
    server.join();

我的项目布局是一个多maven项目(使用intelli J),布局是这样的:

/myapp/src/main/java/main.java (this contains the above code to start jetty)
/myapp/src/main/webapp
/myapp/src/main/webapp/assets
/myapp/src/main/webapp/WEB-INF
/myapp/src/main/webapp/WEB-INF/web-context.xml (spring config file)
/myapp/src/main/webapp/WEB-INF/web.xml
/myapp/src/main/webapp/WEB-INF/views/ (parent folder for my freemarker template files)
/myapp/src/main/webapp/WEB-INF/views/home/index.ftl 

我的 web.xml 是:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath*:log4j.properties</param-value>
</context-param>

 <servlet>
    <servlet-name>myapp</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/web-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>myapp</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

当我在 IntelliJ (11 ulimitate) 中运行它时,我得到以下输出:

2012-08-15 19:17:11,611 [main] INFO  org.eclipse.jetty.server.Server - jetty-7.6.2.v20120308
2012-08-15 19:17:11,886 [main] INFO  org.eclipse.jetty.webapp.StandardDescriptorProcessor - NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
2012-08-15 19:17:11,962 [main] INFO  org.eclipse.jetty.server.handler.ContextHandler - started o.e.j.w.WebAppContext{/,file:/Users/me/projects/myapp/myapp-web/target/classes/}
2012-08-15 19:17:12,021 [main] INFO  org.eclipse.jetty.server.AbstractConnector - Started SelectChannelConnector@0.0.0.0:8080

这显然是行不通的,因为当我使用带有 IntelliJ 的 tomcat 运行它时,我得到了一个巨大的输出,比如休眠、弹簧等。

我的 web 模块的 pom.xml 有:

..
 <packaging>war</packaging>
..

【问题讨论】:

  • 看看这是否有帮助(它有效)-> stackoverflow.com/a/10609507/169277
  • 那是为了生产对吧?不是为了本地发展?我看到 scanInterval 看起来像是一个开发设置(如果它不同?)
  • 我不想在开发模式下运行码头,这是为了生产。
  • 您要运行分解的 WAR 还是 WAR 本身?无论哪种情况,这些资源的绝对路径是什么,您是否确认URL location 指向那里?

标签: java spring spring-mvc jetty


【解决方案1】:

如果您的目标是在生产中运行它,并且您已经使用 maven 来构建您的应用程序,我建议您首先创建实际的 war 包:

mvn clean package

然后,一旦您构建了 web 应用程序,就创建一个新的 Jetty 服务器,该服务器从战争档案(而不是源代码)运行。

正如 Jetty manual 所说,它应该是这样的:

    Server server = new Server(8080);

    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar("path_to_the_war_file/your_app.war");
    server.setHandler(webapp);

    server.start();
    server.join();

【讨论】:

  • war包类型和jar包类型有区别吗?
  • 从技术上讲,war 和 jar 都是类似 .zip 的文件(它们使用 zip 压缩格式)。 Jar 是 java 类(和一些元数据)的集合,通常形成一个 java 库,但是 war 应该是一个 web 应用程序存档。通常war存档包含web静态资源、java类文件、库(即jars)、容器配置文件和元数据。
  • @Blankman 在任何情况下你的码头都能够运行一个战争打包的应用程序或它的爆炸版本。问题是您将资源库设置为您的 src directroywebAppContext.setResourceBase(location.toExternalForm()),这是错误的。资源库是一个包含静态资源的目录(具有 maven 结构 -> src/main/webapp)。此外,如果您想就地运行它,请配置您的 IntelliJ 以将类编译为 src/main/webapp/WEB-INF/classes。希望这会有所帮助
  • @MarekDec 好点,他似乎也引用了 web.xml 错误,不是吗?
  • @MarekDec 所以我运行了 mvn clean package,我的 .war 位于 target/myapp.war 如何在本地测试和生产环境中引用该位置?
【解决方案2】:

您可能缺少上下文加载器侦听器。

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

【讨论】:

  • 所以在做嵌入式码头时需要这个,而不是使用tomcat? (当我通过 intellij 使用 tomcat 运行它时它可以工作)
  • 无论如何都应该需要它。这就是启动你的 spring 配置的原因。
  • 所以当我使用 IntelliJ 的 tomcat 设置运行应用程序时它工作正常,并且我的 web.xml 中没有侦听器。
【解决方案3】:

不要从 main.java 手动调用 Jetty,而是参考 pom.xml 中的 Maven Jetty 插件,并通过运行 mvn jetty:run 来启动您的应用程序

<build>
  <plugins>
    <plugin>
      <groupId>org.mortbay.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>8.1.4.v20120524</version>
    </plugin>
  </plugin>
</build>

如果您的目标是创建一个带有 Jetty 嵌入式副本的可执行 WAR 文件,您可能希望这样处理任务:

    final Connector connector = new SelectChannelConnector();
    connector.setPort(8080);

    final Server server = new Server();
    server.addConnector(connector);
    server.setStopAtShutdown(true);

    final WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    context.setServer(server);

    final ProtectionDomain protectionDomain = Main.class.getProtectionDomain();
    final URL location = protectionDomain.getCodeSource().getLocation();
    context.setWar(location.toExternalForm());
    server.addHandler(context);

    server.start();
    server.join();

【讨论】:

    【解决方案4】:

    你有没有试过设置defaultsDescriptor参数

    webAppContext.setDefaultsDescriptor(JETTY_HOME+"/etc/webdefault.xml");
    

    JETTY_HOME 是你安装 jetty 的地方,你可以找到 JETTY_HOME/etc/webdefault.xml 包含必要的设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2017-09-19
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      相关资源
      最近更新 更多