简体   繁体   English

来自IBM WebSphere Application Server上的WebJars的静态资源不可见

[英]Static resources from WebJars on IBM WebSphere Application Server not visible

I'm writing a simple reporting web application using JAX-RS and AngularJS deployed to IBM WebSphere Application Server. 我正在使用部署到IBM WebSphere Application Server的JAX-RS和AngularJS编写一个简单的报表Web应用程序。 Since it is really very simple I try to keep as little dependencies as possible, so I am not using any fancy framework server-side. 因为它非常简单,所以我尽量保持尽可能少的依赖关系,所以我没有使用任何花哨的框架服务器端。

For serving static resources I wrote this controller. 为了提供静态资源,我写了这个控制器。 I keep them in src/main/resources folder. 我将它们保存在src/main/resources文件夹中。

@Path("/")
public class Assets {

    @GET
    @Path("{path:.*\\.js}")
    public Response javascript(@PathParam("path") String resourceName) {
        return serveResource(resourceName, "application/javascript");
    }

    private Response serveResource(String resourceName, String contentType) {
        InputStream inputStream =
            getClass().getClassLoader().getResourceAsStream(resourceName);

        return Response.ok(inputStream)
                .header(HttpHeaders.CONTENT_TYPE, contentType)
                .build();
    }
}

So far so good. 到现在为止还挺好。

As I use several JavaScript libraries (jQuery, Underscore.js, AngularJS), I wanted to make use of WebJars . 当我使用几个JavaScript库(jQuery,Underscore.js,AngularJS)时,我想使用WebJars According to documentation on webjars.org: 根据文档上webjars.org:

With any Servlet 3 compatible container, the WebJars that are in the WEB-INF/lib directory are automatically made available as static resources. 对于任何与Servlet 3兼容的容器, WEB-INF/lib目录中的WebJars将自动作为静态资源提供。 This works because anything in a META-INF/resources directory in a JAR in WEB-INF/lib is automatically exposed as a static resource. 这是有效的,因为WEB-INF/lib JAR中的META-INF/resources目录中的任何内容都会自动公开为静态资源。

When I put corresponding WebJars into my pom.xml , things stopped working. 当我将相应的WebJars放入我的pom.xml ,事情就停止了。 This is what I found out so far: 这是我到目前为止发现的:

  • WebJars are, of course, present in the resulting .war package, in WEB-INF/lib folder. 当然,WebJars存在于生成的.war包中,位于WEB-INF/lib文件夹中。
  • ClassLoader used in my serveResource method claims in has all WebJars in its classpath. 我的serveResource方法声明中使用的ClassLoader在其类路径中包含所有WebJars。
  • The ClassLoader 's implementation is com.ibm.ws.classloader.CompoundClassLoader . ClassLoader的实现是com.ibm.ws.classloader.CompoundClassLoader
  • None of the resources in present in WebJars in META-INF/resources folder is visible to the ClassLoader . 资源没有在存在于WebJars META-INF/resources文件夹是与可见ClassLoader None. 没有。

How can I make the class loader see the resources in the WebJars? 如何让类加载器看到WebJars中的资源?

For serving files form JAR META-INF/resources you need WebSphere v8.5 . WebSphere v8.5 JAR META-INF/resources提供文件,您需要WebSphere v8.5
Check this page for more details - Web container configuration for JavaServer Pages static file access 查看此页面以获取更多详细信息 - JavaServer Pages静态文件访问的Web容器配置

Use the following methods to locate your static files: 使用以下方法查找静态文件:

URL ServletContext.getResource(String path)
Set ServletContext.getResourcePaths(String path) 

META-INF/resources directories of fragments under the WEB-INF/Lib directory in the application WAR file 应用程序WAR文件中WEB-INF / Lib目录下的片段的META-INF / resources目录

After searching pre-fragment document roots, the web container searches web fragments. 在搜索预片段文档根之后,Web容器搜索Web片段。 A web fragment comprises a JAR file in an application WEB-INF/lib directory. Web片段包括应用程序WEB-INF / lib目录中的JAR文件。 The JAR might include static resources in a META-INF/resources directory that are defined within the JAR file. JAR可能包含在JAR文件中定义的META-INF / resources目录中的静态资源。 To prevent the web container from searching META-INF/resources directories, set the com.ibm.ws.webcontainer.SkipMetaInfResourcesProcessing web container custom property to true. 要阻止Web容器搜索META-INF / resources目录,请将com.ibm.ws.webcontainer.SkipMetaInfResourcesProcessing Web容器定制属性设置为true。 The default value for the custom property is false . custom属性的默认值为false

 com.ibm.ws.webcontainer.SkipMetaInfResourcesProcessing = true 

UPDATE UPDATE
Try to change your method to something like this (pseudo code, no exception handling): 尝试将您的方法更改为此类(伪代码,无异常处理):

public Response javascript(@PathParam("path") String resourceName, 
                           @Context ServletContext servletContext) {

    // make sure resource name starts with / and should be relative path to META-INF/resources
    URL url = serveltContext.getResource(resourceName);  
    InputStream inputStream = url.openStream();

    return Response.ok(inputStream)
            .header(HttpHeaders.CONTENT_TYPE, "application/javascript")
            .build();
}

See ServletContext.getResource and URL.openStream 请参阅ServletContext.getResourceURL.openStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在将应用程序从传统的WebSphere Application Server迁移到Liberty时,如何正确重构ibm-ejb-jar-bnd.xmi文件? - How to correctly refactor ibm-ejb-jar-bnd.xmi file while doing app migration from WebSphere Application Server traditional to Liberty? 在IBM WebSphere Application Server Community Edition中安装新的war文件时,部署失败 - Deployment failed while installing a new war file in IBM WebSphere Application Server Community edition 在IBM Websphere环境中设置负载平衡器服务器 - settingup load balancer server in IBM Websphere environment Websphere Application Server中需要Websphere MQ类 - Websphere MQ classes needed in Websphere Application Server Websphere Application Server从轴jar调用类 - Websphere Application Server calls classes from axis jar WebSphere Application Server免费版 - WebSphere Application Server free edition 会话管理-Websphere Application Server - Session Management - Websphere Application Server 在WebSphere Application Server中升级JavaMail - Upgrade JavaMail in WebSphere Application Server 无法使用IBM Rational Application Developer将WAR文件部署到Websphere - Unable to deploy WAR file to Websphere using IBM Rational Application Developer 使用Websphere Libertyty应用程序从经典Websphere应用程序访问远程ejb - Accessing remote ejb from classic websphere application with websphere liberty application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM