【问题标题】:Convert a context.xml from Tomcat to Jetty将 context.xml 从 Tomcat 转换为 Jetty
【发布时间】:2011-02-08 09:18:56
【问题描述】:

我在 webapp/META-INF/ 中有以下 context.xml。 tomcat 使用这个来定义 Spring 可以通过 Property 理解的值

<?xml version="1.0" encoding="UTF-8"?>
<Context>     
<Parameter name="si.host" value="super.com"  override="false"/>   
</Context>

现在我正在尝试使用 maven jetty 插件部署 webapp:

    <plugin>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>maven-jetty-plugin</artifactId>
    <version>6.1.26</version>
    <configuration>
      <connectors>
        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
          <port>8080</port>
          <maxIdleTime>60000</maxIdleTime>
        </connector>
      </connectors>
      <jettyEnvXml>${basedir}\src\test\resources\server\jetty\jetty-env.xml</jettyEnvXml>
      <jettyConfig>${basedir}\src\test\resources\server\jetty\jetty.xml</jettyConfig > 
       <contextPath>/myapp</contextPath>
      <webApp>target/myapp.war</webApp>
      <stopKey>foo</stopKey>
      <stopPort>9999</stopPort>
    </configuration>
    <executions>
      <execution>
        <id>start-jetty</id>
        <phase>pre-integration-test</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <scanIntervalSeconds>0</scanIntervalSeconds>
          <daemon>true</daemon>
        </configuration>
      </execution>
      <execution>
        <id>stop-jetty</id>
        <phase>post-integration-test</phase>
        <goals>
          <goal>stop</goal>
        </goals>
      </execution>
    </executions>        
  </plugin>

如何在 jetty.xml 文件中添加此参数?
我已经深入研究了他们的文档以及此处和谷歌,但没有发现任何明确的内容。
提前感谢您的帮助。

【问题讨论】:

    标签: tomcat jetty


    【解决方案1】:

    这需要进入你的WEB-INF/web.xml,它独立于网络服务器,即它可以在 tomcat 和 jetty 中工作:

    <context-param>
      <param-name>si.host</param-name>
      <param-value>super.com</param-value>
    </context-param>
    

    或者您可以像这样在您的码头 xml 中设置它:

    <Configure class="org.mortbay.jetty.webapp.WebAppContext">
      ...
      <Set name="initParams">
        <Map>
          <Entry>
            <Item>si.host</Item>
            <Item>super.com</Item>
          </Entry>
        </Map>
      </Set>
    </Configure>
    

    【讨论】:

    • 有没有办法在外部提供这个?因为 webapp 不仅部署在码头上,而且部署在带有货物的 tomcat 中。如果我在 web.xml 和 context.xml 中定义了这个,并且它部署在一个 tomcat 中,使用哪个值?
    • 你应该把它从 context.xml 中删除,只保留在你的 web.xml 中。
    【解决方案2】:

    首先谢谢DogBane!!

    我找到了其他解决方案,无需修改 web.xml 使用默认 web.xml 文件。

    此文件在它自己的 WEB_INF/web.xml 文件之前应用于 Web 应用程序

    我更喜欢这个而不是使用 jetty.xml,因为它不那么冗长。

    只需在 maven 插件配置部分添加 webDefaultXml 以及以下文件的路径:

    <web-app
       xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns   /javaee/web-app_2_5.xsd"
       metadata-complete="true"
       version="2.5">
    
      <description>
        Default web.xml file.
        This file is applied to a Web application before it's own WEB_INF/web.xml file
      </description>
    
       <context-param>
        <param-name>si.host</param-name>
          <param-value>super.com</param-value>
       </context-param>
    </web-app>
    

    【讨论】:

    • context-param 在我的情况下被忽略。我正在使用 Jetty 9。
    猜你喜欢
    • 1970-01-01
    • 2011-02-18
    • 2016-11-25
    • 1970-01-01
    • 2015-09-09
    • 2017-10-07
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多