有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Tomcat自定义上下文。xml文件从未被考虑在内

我目前对web应用程序的Tomcat配置有问题(我使用Tomcat 8.5.57)
这个web应用程序打包在一个war文件中,其中包括html文件和css文件
这个应用程序运行良好

现在,我收到一位客户的请求,要求能够通过客户管理的定制css文件(用于设置客户的徽标或类似的东西),从战争之外修改应用程序的外观和感觉
因此,我尝试创建一个名为custom.xml的自定义上下文文件,并将其放置在tomcat\conf\Catalina\localhost目录中
此文件看起来像:

<Context    docBase="E:/somedirectory/support" 
            path="/app/css" 
            reloadable    = "false"
            unpackWAR     = "false"
            swallowOutput = "true" >
                <WatchedResource>custom.css</WatchedResource>           
</Context>

我将包含一些css指令的custom.css文件作为测试放在E:/somedirectory/support目录中
在我的web应用程序的html文件中,标题部分有以下行:

<link rel="stylesheet" href="css/custom.css" media="screen" type="text/css"/>

问题是我的custom.css文件从未被考虑在内

当我打开Chrome开发者工具的Sources选项卡时,我看到了custom.css层次结构中的一个app/css文件,正如预期的那样(可能是由于html文件中的行),但它是空的,令人绝望
我尝试了很多在网络和stackoverflow上找到的东西,但都不管用

有人能帮我吗

谢谢!


共 (1) 个答案

  1. # 1 楼答案

    <Context>元素的path属性在server.xml之外被忽略:

    This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase. [from Tomcat documentation]

    因此,你有两个选择:

    • 通过创建名为conf\Catalina\localhost\app#css.xml的文件和内容,可以使用上下文路径/app/css定义新的上下文(新的web应用程序):
    <Context docBase="E:\somedirectory\support" />
    

    这样,/app/css子树下的所有内容都将只从E:\somedirectory\support目录提供服务

    1. 通过添加一个名为conf\Catalina\localhost\app.xml的文件,可以重新定义应用程序上下文,以包含一个附加的虚拟目录(WAR文件的内容旁边):
    <Context>
      <Resources>
        <PreResources className="org.apache.catalina.webresources.DirResourceSet"
                      base="E:\somedirectory\support"
                      webAppMount="/css" />
      </Resources>
    </Context>
    

    这样,在处理/app/css/foo/bar请求时,Tomcat将首先在E:\somedirectory\support中查找foo/bar,然后在WAR文件中查找