NutzCN Logo
问答 boot结合freemaker,template文件都在jar包中,如何正确配置资源路径?
发布于 2404天前 作者 七星爆珠 6518 次浏览 复制 上一个帖子 下一个帖子
标签:

在gradle中仅此一个与freemaker有关的依赖配置

compile group: 'org.nutz', name: 'nutzboot-starter-freemarker', version: '2.1.5'

在application.properties中有关配置

freemarker.suffix=.ftl

模板文件资源位置:src-main-resources-template
在ide中跑是正常的,可以正确显示模板内容在client端,但是打包发布到生产环境会抛出以下异常:

org.nutz.ioc.IocException: IocBean[webHomeModule] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:159)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: IocBean[webHomeModule] fail at field=[freeMarkerConfigurer]
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
	at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
	... 35 common frames omitted
Caused by: org.nutz.ioc.IocException: IocBean[freeMarkerConfigurer] throw Exception when creating
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:60)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
	... 37 common frames omitted
Caused by: java.lang.NullPointerException: null
	at freemarker.cache.FileTemplateLoader$1.run(FileTemplateLoader.java:121)
	at freemarker.cache.FileTemplateLoader$1.run(FileTemplateLoader.java:119)
	at java.security.AccessController.doPrivileged(Native Method)
	at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:119)
	at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:98)
	at freemarker.template.Configuration.setDirectoryForTemplateLoading(Configuration.java:1481)
	at org.nutz.boot.starter.freemarker.FreeMarkerConfigurer.initFreeMarkerConfigurer(FreeMarkerConfigurer.java:110)
	at org.nutz.boot.starter.freemarker.FreeMarkerConfigurer.init(FreeMarkerConfigurer.java:75)
	at org.nutz.boot.starter.freemarker.FreeMarkerConfigurer$FM$init$62febe830a4a6764c634d9e263964cee.invoke(FreeMarkerConfigurer.java)
	at org.nutz.ioc.impl.ObjectMakerImpl$2.trigger(ObjectMakerImpl.java:180)
	at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:89)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:141)
	... 40 common frames omitted

看了以下fm的文档,有关资源读取的方式如下

Built-in template loaders
void setDirectoryForTemplateLoading(File dir):
void setClassForTemplateLoading(Class cl, String basePackagePath)
void setServletContextForTemplateLoading(Object servletContext, String path)

又看了下starter初始化源码

protected void initFreeMarkerConfigurer() throws IOException, TemplateException {
		String path = freemarkerDirectiveFactory.getFreemarker();
		File file = Files.findFile(path);
		if (!Lang.isEmpty(file)) {
			Properties p = new Properties();
			p.load(Streams.fileIn(file));
			configuration.setSettings(p);
		}
		File f = Files.findFile(prefix);
		configuration.setDirectoryForTemplateLoading(f);
	}

貌似只提供了一种读取的方式,请问该如何解决?谢谢

14 回复

路过,围观一波。

看上去是bug,来报个issue

额 感觉等更新版本有点着急 有什么临时补救方法吗?

可以把template拷贝一下,跟jar放在同一个文件夹试试

debug一下,看看在idea环境下f的指是什么

是template文件系统的绝对路径 路径也是正确的

protected void initFreeMarkerConfigurer() throws IOException, TemplateException {
		String path = freemarkerDirectiveFactory.getFreemarker();
		File file = Files.findFile(path);
		if (!Lang.isEmpty(file)) {
			Properties p = new Properties();
			p.load(Streams.fileIn(file));
			configuration.setSettings(p);
		}
		File f = Files.findFile(prefix);---------------------------------感觉这里在生产环境下应该是空,资源在jar文件中
		configuration.setDirectoryForTemplateLoading(f);
	}

那一行里面,prefix的值是多少呢?

如果启动时的文件夹也是在jar所在的文件夹,然后template文件夹也在jar所在的文件夹,应该是能找到的

prefix = /D:/work/ProjectA/02_modulet/out/production/resources/template

有个终极版本,把FreeMarkerConfigurer源码拷贝过,相同package下,优先级比jar高,直接改之

可以可以 这个终极大招果然好使!!!只修改了一行代码,ide和生产环境下就都跑通了

configuration.setClassForTemplateLoading(getClass(),"template");

嗯嗯, 记得报个issue

添加回复
请先登陆
回到顶部