开发者社区> 问答> 正文

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

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

在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)

展开
收起
爱吃鱼的程序员 2020-06-09 16:09:20 1095 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    使用ClasspathLoader 

    ClasPathLoaderclassPathLoader=newClasPathLoader("templates/")beetlGroupUtilConfiguration.setResourceLoader(classPathLoader);



    假设你的模板在templates目录下


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

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

    http://git.oschina.net/xiandafu/springboot_beetl_beetlsql参考这个工程 @闲大赋  这个问题在切换成springboot1.4版本后依然存在。

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

    使用ClasspathLoader 

    ClasPathLoaderclassPathLoader=newClasPathLoader("templates/")beetlGroupUtilConfiguration.setResourceLoader(classPathLoader);



    假设你的模板在templates目录下


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

    java.io.FileNotFoundException:classpathresource[resources/]cannotberesolvedtoabsolutefilepathbecauseitdoesnotresideinthefilesystem:jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/lib/ant-javafx.jar!/resources/

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

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

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

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

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

       @Bean(initMethod="init",name="beetlConfig")
       publicBeetlGroupUtilConfigurationgetBeetlGroupUtilConfiguration(){
          BeetlGroupUtilConfigurationbeetlGroupUtilConfiguration=newBeetlGroupUtilConfiguration();
          ClasspathResourceLoaderclassPathLoader=newClasspathResourceLoader(this.getClass().getClassLoader(),
                resourceRoot);
          beetlGroupUtilConfiguration.setResourceLoader(classPathLoader);
          //读取配置文件信息
          ResourcePatternResolverpatternResolver=ResourcePatternUtils
                .getResourcePatternResolver(newDefaultResourceLoader());
          beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));
          returnbeetlGroupUtilConfiguration;
       }

       @Bean(name="beetlViewResolver")
       publicBeetlSpringViewResolvergetBeetlSpringViewResolver(
             @Qualifier("beetlConfig")BeetlGroupUtilConfigurationbeetlGroupUtilConfiguration){
          BeetlSpringViewResolverbeetlSpringViewResolver=newBeetlSpringViewResolver();
          beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
          //beetlSpringViewResolver.setPrefix("/");
          beetlSpringViewResolver.setSuffix(".html");
          beetlSpringViewResolver.setOrder(0);
          beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
          returnbeetlSpringViewResolver;
       }

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

    2020-06-09 16:09:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里邮箱—安全高效集成 立即下载
集成智能接入网关APP:优化企业级移动办公网络 立即下载
云效助力企业集成安全到DevOps中 立即下载

相关实验场景

更多