From c97800082a93379027727cb30c00d46eb8ed6221 Mon Sep 17 00:00:00 2001 From: "majianle@qq.com" Date: Sun, 2 Dec 2018 11:28:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DReourceMangerKit=E4=B8=AD?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E8=B7=AF=E5=BE=84=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=88=A4=E6=96=AD=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=9A=84=E5=AE=9E=E9=99=85=E8=BF=90=E8=A1=8C=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=EF=BC=8C=E8=A7=A3=E5=86=B3Freemarker=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=89=BE=E4=B8=8D=E5=88=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/undertow/ResourceManagerKit.java | 131 +++++++++--------- 1 file changed, 66 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/jfinal/server/undertow/ResourceManagerKit.java b/src/main/java/com/jfinal/server/undertow/ResourceManagerKit.java index f86cc7d..c2beaf9 100644 --- a/src/main/java/com/jfinal/server/undertow/ResourceManagerKit.java +++ b/src/main/java/com/jfinal/server/undertow/ResourceManagerKit.java @@ -1,12 +1,12 @@ /** * Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com). - * + *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,84 +16,85 @@ package com.jfinal.server.undertow; -import java.io.File; import io.undertow.server.handlers.resource.ClassPathResourceManager; import io.undertow.server.handlers.resource.FileResourceManager; import io.undertow.server.handlers.resource.ResourceManager; +import java.io.File; + /** * 1:FileResourceManager 支持在普通目录下去加载 css、js 等 web 静态资源 - * + * * 2:ClassPathResourceManager 支持在 class path 下与 jar 包内加载 css、js * 等 web 静态资源 - * + * * 3:ClassPathResourceManager 第二个参数不能省略,也不能为 "",否则可以在浏览器地址栏直接访问 * class path、jar 中的所有资源,包括配置文件,下面的用法极其危险: * new ClassPathResourceManager(getClassLoader())); - * + * * 4:在需要用到 ClassPathResourceManager 时为给定一个参数,例如: * new ClassPathResourceManager(getClassLoader(), "webapp")); - * + * * 上例给定 webapp 参数,undertow 将可以从 class path、jar 包内的 * webapp 之下去寻找资源 - * + * */ public class ResourceManagerKit { - - public static ResourceManager buildResourceManager(String resourcePath, ClassLoader classLoader) { - CompositeResourceManager ret = new CompositeResourceManager(); - String resourcePathArray[] = resourcePath.split(","); - /** - * 开发时使用 eclipse 启动将会正确添加 FileResourceManager - * 执行 java -jar xxx.jar 命令时,当前目录下面如果存在 path 目录将会被添加 - * - * 经测试 eclipse 启动项目的当前目录值为 APP_BASE - */ - for (String path : resourcePathArray) { - path = path.trim(); - if (new File(path).isDirectory()) { - ret.add(new FileResourceManager(new File(path))); - } - } - - if (UndertowKit.isDeployMode()) { - forDeployMode(classLoader, ret); - } - - return ret; - } - - /** - * 部署模式下需要额外搜索目录用于 FileResourceManager - * 额外添加 webapp、static 为前缀的 ClassPathResourceManager 到最末尾 - */ - private static void forDeployMode(ClassLoader classLoader, CompositeResourceManager ret) { - /** - * 如果前方没有 FileResourceManager 被添加成功,搜索可能存在的 webapp 目录 - */ - if (ret.isEmpty()) { - String path = PathKitExt.getLocationPath(); - if (path.endsWith(File.separatorChar + "lib")) { - path = path.substring(0, path.lastIndexOf(File.separatorChar)); - } - path = new File(path + File.separator + "webapp").getAbsolutePath(); - - if (new File(path).isDirectory()) { - ret.add(new FileResourceManager(new File(path))); - } - } - - /** - * 额外添加一个指向 webapp 的 ClassPathResourceManager - * 该 webapp 内部是 css、js 等这类 web 资源,该目录将被打包 - * 到 jar 之中去 - * - * 如果有人习惯使用使用 static 作为目录,还需要开启最下方的代码 - */ - ret.add(new ClassPathResourceManager(classLoader, "webapp")); - - // ret.add(new ClassPathResourceManager(classLoader, "static")); - } + + public static ResourceManager buildResourceManager(String resourcePath, ClassLoader classLoader) { + CompositeResourceManager ret = new CompositeResourceManager(); + String resourcePathArray[] = resourcePath.split(","); + /** + * 开发时使用 eclipse 启动将会正确添加 FileResourceManager + * 执行 java -jar xxx.jar 命令时,当前目录下面如果存在 path 目录将会被添加 + * + * 经测试 eclipse 启动项目的当前目录值为 APP_BASE + */ + for (String path : resourcePathArray) { + path = PathKitExt.getWebRootPath() + "/" + path.trim(); + if (new File(path).isDirectory()) { + ret.add(new FileResourceManager(new File(path))); + } + } + + if (UndertowKit.isDeployMode()) { + forDeployMode(classLoader, ret); + } + + return ret; + } + + /** + * 部署模式下需要额外搜索目录用于 FileResourceManager + * 额外添加 webapp、static 为前缀的 ClassPathResourceManager 到最末尾 + */ + private static void forDeployMode(ClassLoader classLoader, CompositeResourceManager ret) { + /** + * 如果前方没有 FileResourceManager 被添加成功,搜索可能存在的 webapp 目录 + */ + if (ret.isEmpty()) { + String path = PathKitExt.getLocationPath(); + if (path.endsWith(File.separatorChar + "lib")) { + path = path.substring(0, path.lastIndexOf(File.separatorChar)); + } + path = new File(path + File.separator + "webapp").getAbsolutePath(); + + if (new File(path).isDirectory()) { + ret.add(new FileResourceManager(new File(path))); + } + } + + /** + * 额外添加一个指向 webapp 的 ClassPathResourceManager + * 该 webapp 内部是 css、js 等这类 web 资源,该目录将被打包 + * 到 jar 之中去 + * + * 如果有人习惯使用使用 static 作为目录,还需要开启最下方的代码 + */ + ret.add(new ClassPathResourceManager(classLoader, "webapp")); + + // ret.add(new ClassPathResourceManager(classLoader, "static")); + } } -- Gitee