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

Snowfox_66 发布于 2016/04/05 20:10
阅读 14K+
收藏 1

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

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

加载中
1
闲大赋
闲大赋

使用 ClasspathLoader  

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



假设你的模板在templates目录下


等待美丽的花儿
等待美丽的花儿
太棒了。。解决了。。我以前都是这么写的ClasspathResourceLoader("/templates")为什么斜杠放后面就解决了呢?
0
闲大赋
闲大赋

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

0
闲大赋
闲大赋
该评论暂时无法显示,详情咨询 QQ 群:点此入群
0
等待美丽的花儿
等待美丽的花儿
@闲大赋  这个问题在切换成springboot 1.4版本后依然存在。
0
闲大赋
闲大赋

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

使用 ClasspathLoader  

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



假设你的模板在templates目录下


java的ClassLoader就是那么干的
0
古岁
古岁

没有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/
	
0
呼呼南风
呼呼南风

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

0
prosoul
prosoul

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

@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;
    }

0
可可逗
可可逗

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

OSCHINA
登录后可查看更多优质内容
返回顶部
顶部