spring boot 集成beetl模板路径找不到的问题!

发布于 2022-01-02 09:48:21 字数 7769 浏览 935 评论 10

@闲大赋 你好,想跟你请教个问题:

在eclipse中,可以正常找到路径。将工程生成jar包后运行就找不到模板路径了。主要代码如下:

public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

	@Bean(initMethod = "init", name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
 
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());
        try {
            // WebAppResourceLoader 配置root路径是关键
            WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource("classpath:/templates/").getFile().getPath());
            beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //读取配置文件信息
        beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));
        return beetlGroupUtilConfiguration;
    }
 
    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setPrefix("/");
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }
beetl.properties
#######默认配置
ENGINE=org.beetl.core.engine.FastRuntimeEngine
DELIMITER_PLACEHOLDER_START=${
DELIMITER_PLACEHOLDER_END=}
DELIMITER_STATEMENT_START=<%
DELIMITER_STATEMENT_END=%>
DIRECT_BYTE_OUTPUT = FALSE
HTML_TAG_SUPPORT = true
HTML_TAG_FLAG = #
HTML_TAG_BINDING_ATTRIBUTE = var
NATIVE_CALL = TRUE
TEMPLATE_CHARSET = UTF-8
ERROR_HANDLER = org.beetl.core.ConsoleErrorHandler
NATIVE_SECUARTY_MANAGER= org.beetl.core.DefaultNativeSecurityManager
#RESOURCE_LOADER=org.beetl.core.resource.ClasspathResourceLoader
#RESOURCE_LOADER=org.beetl.core.resource.WebAppResourceLoader.WebAppResourceLoader(
MVC_STRICT = FALSE

### 资源配置,resource后的属性只限于特定ResourceLoader ####
#classpath 跟路径
#RESOURCE.root= /
#是否检测文件变化
RESOURCE.autoCheck = TRUE
#自定义脚本方法文件位置
RESOURCE.functionRoot = functions
#自定义脚本方法文件的后缀
RESOURCE.functionSuffix = html
#自定义标签文件位置
RESOURCE.tagRoot = htmltag
#自定义标签文件后缀
RESOURCE.tagSuffix = tag


#####  扩展 ##############
## 内置的方法
FN.date = org.beetl.ext.fn.DateFunction
FN.nvl = org.beetl.ext.fn.NVLFunction
FN.debug = org.beetl.ext.fn.DebugFunction
#兼容以前版本,用has代替
FN.exist = org.beetl.ext.fn.CheckExistFunction
FN.has = org.beetl.ext.fn.CheckExistFunction
FN.printf = org.beetl.ext.fn.Printf
FN.decode = org.beetl.ext.fn.DecodeFunction
FN.assert = org.beetl.ext.fn.AssertFunction
FN.print = org.beetl.ext.fn.Print
FN.println = org.beetl.ext.fn.Println
FN.trunc = org.beetl.ext.fn.TruncFunction
#兼容以前版本 empty,用isEmpty代替
FN.empty = org.beetl.ext.fn.EmptyFunction
FN.qmark = org.beetl.ext.fn.QuestionMark
FN.isEmpty = org.beetl.ext.fn.EmptyExpressionFunction
FN.parseInt = org.beetl.ext.fn.ParseInt
FN.parseLong = org.beetl.ext.fn.ParseLong
FN.parseDouble= org.beetl.ext.fn.ParseDouble
FN.range = org.beetl.ext.fn.Range
FN.flush = org.beetl.ext.fn.Flush
FN.json = org.beetl.ext.fn.Json

##内置的功能包
FNP.strutil = org.beetl.ext.fn.StringUtil
FNP.array = org.beetl.ext.fn.ArrayUtil

##内置的格式化函数
FT.dateFormat =  org.beetl.ext.format.DateFormat
FT.numberFormat =  org.beetl.ext.format.NumberFormat
##内置的默认格式化函数
FTC.java.util.Date = org.beetl.ext.format.DateFormat
FTC.java.sql.Date = org.beetl.ext.format.DateFormat
FTC.java.sql.Time = org.beetl.ext.format.DateFormat
FTC.java.sql.Timestamp = org.beetl.ext.format.DateFormat
FTC.java.lang.Short = org.beetl.ext.format.NumberFormat
FTC.java.lang.Long = org.beetl.ext.format.NumberFormat
FTC.java.lang.Integer = org.beetl.ext.format.NumberFormat
FTC.java.lang.Float = org.beetl.ext.format.NumberFormat
FTC.java.lang.Double = org.beetl.ext.format.NumberFormat
FTC.java.math.BigInteger = org.beetl.ext.format.NumberFormat
FTC.java.math.BigDecimal = org.beetl.ext.format.NumberFormat
FTC.java.util.concurrent.atomic.AtomicLong = org.beetl.ext.format.NumberFormat
FTC.java.util.concurrent.atomic.AtomicInteger = org.beetl.ext.format.NumberFormat

##虚拟属性 无
## 标签类
TAG.include= org.beetl.ext.tag.IncludeTag
TAG.includeFileTemplate= org.beetl.ext.tag.IncludeTag
TAG.layout= org.beetl.ext.tag.LayoutTag
TAG.delete= org.beetl.ext.tag.DeleteTag
TAG.htmltag= org.beetl.ext.tag.HTMLTagSupportWrapper
TAG.htmltagvar= org.beetl.ext.tag.HTMLTagVarBindingWrapper
TAG.cache= org.beetl.ext.tag.cache.CacheTag
报错信息如下:
2016-04-05 20:04:10.494  INFO 6876 --- [           main] org.hibernate.tool.hbm2ddl.SchemaExport  : HHH000230: Schema export complete
java.io.FileNotFoundException: class path resource [templates/] cannot be resolved to absolute file path because it does not reside in the file system
: jar:file:/D:/Workspace/SpringBootExample/target/SpringBootExample-1.0.jar!/templates/
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:218)
        at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
        at com.onon.example.Application.getBeetlGroupUtilConfiguration(Application.java:33)
        at com.onon.example.Application$$EnhancerBySpringCGLIB$$90574f40.CGLIB$getBeetlGroupUtilConfiguration$0(<generated>)
        at com.onon.example.Application$$EnhancerBySpringCGLIB$$90574f40$$FastClassBySpringCGLIB$$fcd5f4f7.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:355)
        at com.onon.example.Application$$EnhancerBySpringCGLIB$$90574f40.getBeetlGroupUtilConfiguration(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFacto
ry.java:1123)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:101
8)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

檐上三寸雪 2022-01-07 23:57:56

你好,请问怎么配置beet模板在webapp路径下?

刘备忘录 2022-01-07 23:57:48

我是这样配置的,可以启动

@Value("${RESOURCE.root:/templates}")
    String resourceRoot;// 模板跟目录

    @Bean(initMethod = "init", name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ClasspathResourceLoader classPathLoader = new ClasspathResourceLoader(this.getClass().getClassLoader(),
                resourceRoot);
        beetlGroupUtilConfiguration.setResourceLoader(classPathLoader);
        // 读取配置文件信息
        ResourcePatternResolver patternResolver = ResourcePatternUtils
                .getResourcePatternResolver(new DefaultResourceLoader());
        beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));
        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(
            @Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        // beetlSpringViewResolver.setPrefix("/");
        beetlSpringViewResolver.setSuffix(".html");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }

毁梦 2022-01-07 23:37:01

写得不错,鼓励一下, 再分享一份资料。

http://www.marsitman.com/springboot/springboot_helloworld.html

把昨日还给我 2022-01-07 23:16:41

请问能不能把你的配置给我看看,我也出现你那个问题了。

清欢 2022-01-07 23:01:56

没有ClassPathLoader这个类,这个类是sun.rmi.rmic.iiop的?我现在能正常运行,但是执行不了单元测试,Maven的打包的时候执行测试都可以通过,手动执行测试就不行,报如下错误:

java.io.FileNotFoundException: class path resource [resources/] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/lib/ant-javafx.jar!/resources/
	
私藏温柔 2022-01-07 22:57:46

引用来自“闲大赋”的评论

使用 ClasspathLoader  

ClasPathLoader classPathLoader= new ClasPathLoader ("templates/")
   beetlGroupUtilConfiguration.setResourceLoader(classPathLoader);

假设你的模板在templates目录下

归属感 2022-01-07 22:48:46


@闲大赋
 这个问题在切换成springboot 1.4版本后依然存在。

等风来 2022-01-07 21:38:28

你的模板在jar中,所以不能用WebResourceLoader(它是按照文件路径加载的),你可以用ClasspathLoader 试试

泪冰清 2022-01-06 03:52:39

太棒了。。解决了。。我以前都是这么写的ClasspathResourceLoader("/templates")为什么斜杠放后面就解决了呢?

惜醉颜 2022-01-05 16:54:37

使用 ClasspathLoader  

ClasPathLoader classPathLoader= new ClasPathLoader ("templates/")
   beetlGroupUtilConfiguration.setResourceLoader(classPathLoader);

假设你的模板在templates目录下

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文