gpt4 book ai didi

java - 使用 Spring MVC 的嵌入式 Jetty

转载 作者:行者123 更新时间:2023-12-04 05:44:55 25 4
gpt4 key购买 nike

我有一个用于我的 spring MVC 和 hibernate 应用程序的多个 maven 模块。

我的布局如下:

/myapp-web
/myapp-web/src/main/java
/myapp-web/src/main/webapp
/myapp-web/src/main/webapp/web-inf
/myapp-web/src/main/webapp/web-inf/web.xml
/myapp-web/src/main/webapp/web-inf/web-context.xml (spring wiring code etc)
/myapp-web/src/main/webapp/web-inf/config/hibernate.cfg.xml
/myapp-web/src/main/webapp/assets/... (folder for css, javascript, images)
/myapp-web/src/main/webapp/views (folders with freemarker .ftl files)

我的 pom.xml:

    ..
<packaging>war</packaging>
..
<dependency>
<artifactId>jetty-server</artifactId>
<groupId>org.eclipse.jetty</groupId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty-version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty-version}</version>
</dependency>

我有另一个嵌入式 jetty 实例,我已成功设置并且可以运行。我什至设置了一个启动/停止脚本,因此它可以在 Ubuntu 上运行。这不是一个 spring mvc 应用程序,所以它更容易,因为它没有 web.xml 文件等。我创建了一个/assembly/assembly.xml 文件,我是否也需要为 spring mvc 应用程序执行此操作?

然后我将其添加到我的 pom.xml 插件部分:

         <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin}</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
<!-- jar plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin}</version>
<configuration>
<archive>

</archive>
</configuration>
</plugin>
<!-- jetty plugin -->
<plugin>
<!-- This plugin is needed for the servlet example -->
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.myapp.HttpServer</mainClass>
</configuration>
</plugin>

我是否需要为我的 spring mvc 应用程序做同样的事情?

我找到了一些 threads在 stackoverflow 上,但我无法理解 ant 部分,因为我没有使用 ant。我也不确定如何移植它以与 Maven 一起工作:

令我惊讶的是,除了单个 servlet 示异常(exception),没有关于如何执行此操作的良好引用。

我是否需要为此创建一个/assembly/assembly.xml 文件,或者对于 Web 应用程序情况有所不同?

有人可以逐步向我解释这个,以便我可以让它工作吗?请询问,我会发布我的项目所需的任何内容,但我认为我涵盖了我当前环境的所有内容,即:

  • 使用多 Maven 设置
  • Spring MVC
  • hibernate
  • 模板的 freemarker
  • 上面给出了文件夹结构

目标是能够部署这个 war 文件,创建启动/停止脚本,这样我就可以将它部署到我的服务器并启动/停止 jetty 实例。

最佳答案

您不需要 assembly.xml 文件。

Jetty 用于启动 webapp,无论是否有弹性。所以,你必须在主类中创建一个 Jetty 服务器,添加你的 webapp 上下文并启动服务器。您可以通过两种方式解决您的问题:- 创建一个类似于“通用”Java EE 服务器的 web.xml,并将此描述符添加到您的 Jetty 服务器。- 使用 servlet 上下文以编程方式在主类中加载 Spring 上下文: https://github.com/s4nchez/jetty-spring-mvc-jar

关于java - 使用 Spring MVC 的嵌入式 Jetty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11285468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com