Jetty没有拿起我的弹簧应用程序

时间:2012-08-15 23:18:40

标签: java spring spring-mvc jetty

我有一个Spring MVC应用程序,我使用hibernate,freemarker。它被设置为多个maven项目。我正在使用IntelliJ终极版。

Jetty开始很好,但是当我去

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

这显然不起作用,因为当我使用tomcat w / intelliJ运行它时,我得到了hibernate,spring等等的巨大输出。

我的网络模块的pom.xml有:

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

4 个答案:

答案 0 :(得分:4)

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

mvn clean package

然后,一旦你构建了webapp,就可以创建一个从war存档(而不是源代码)运行的新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();

答案 1 :(得分: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>

答案 2 :(得分:0)

不是从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();

答案 3 :(得分:0)

您是否尝试过设置defaultsDescriptor参数

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

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

相关问题