Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 码头是';我没有收到我的spring应用程序_Java_Spring_Spring Mvc_Jetty - Fatal编程技术网

Java 码头是';我没有收到我的spring应用程序

Java 码头是';我没有收到我的spring应用程序,java,spring,spring-mvc,jetty,Java,Spring,Spring Mvc,Jetty,我有一个SpringMVC应用程序,我使用hibernate,freemarker。它被设置为一个多maven项目。我正在使用IntelliJ ultimate 码头开始很好,但当我去 http://localhost:8080/ 它只输出我的项目文件夹,我可以在浏览器中查看我的源代码 以下是我当前的设置: final Server server = new Server(8080); ProtectionDomain domain = HttpServer.class.ge

我有一个SpringMVC应用程序,我使用hibernate,freemarker。它被设置为一个多maven项目。我正在使用IntelliJ ultimate

码头开始很好,但当我去

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>
这显然不起作用,因为当我使用tomcatw/intelliJ运行它时,我会得到一个巨大的输出,比如hibernate、spring等等

web模块的My pom.xml具有:

..
 <packaging>war</packaging>
..
。。
战争
..

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

<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>

org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/applicationContext.xml

如果您打算在生产环境中运行此功能,并且已经使用maven构建了应用程序,我建议您首先创建实际的war包:

mvn clean package
然后,在构建webapp后,创建一个新的Jetty服务器,该服务器从war存档(而不是源代码)运行

正如码头所述,它应该是这样的:

    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();

不要从
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>

您是否尝试设置defaultsDescriptor参数

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

JETTY_HOME是您安装JETTY的地方,您可以找到JETTY_HOME/etc/webdefault.xml包含基本设置。

看看这是否有帮助(它可以工作)->这是用于生产的,对吗?不是为了地方发展?我看到scanInterval,所以它看起来像一个开发设置(如果它不同的话?)我不想在开发模式下运行jetty,这是为了生产。你想运行一场爆炸战争还是战争本身?在这两种情况下,这些资源的绝对路径是什么?您是否确认
URL位置
指向那里?因此,在执行嵌入式jetty时需要这样做,而不是使用tomcat?(当我通过intellij使用tomcat运行它时,它就工作了)无论发生什么情况,都应该需要它。这就是启动spring配置的原因。因此,当我使用IntelliJ的tomcat设置运行应用程序时,它可以正常工作,并且我的web.xml中没有listner。war和jar包类型之间有什么区别吗?从技术上讲,war和jar都是类似.zip的文件(它们使用zip压缩格式)。Jar是java类(和一些元数据)的集合,通常形成java库,而war应该是web应用程序归档。通常war存档包含web静态资源、java类文件、库(即JAR)、容器配置文件和元数据。@Blankman在任何情况下,您的jetty都能够运行war打包的应用程序或其分解版本。问题在于,您将资源库设置为src directroywebAppContext.setResourceBase(location.toExternalForm()),这是错误的。资源库是一个包含静态资源的目录(使用maven structure->src/main/webapp)。另外,如果您想就地运行它,请配置IntelliJ以将类编译为src/main/webapp/WEB-INF/classes。希望如此helps@MarekDec很好,他似乎也引用了web.xml错误的位置没有?@MarekDec,所以我运行了mvn clean package,我的.war位于target/myapp.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();
webAppContext.setDefaultsDescriptor(JETTY_HOME+"/etc/webdefault.xml");