NutzCN Logo
问答 jetty打包运行报错了
发布于 2171天前 作者 shuxinyun 1684 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

我通过java代码调用beetl模板文件,代码如下:

public static Template getTemplate(String data,String type) {
        GroupTemplate gt=null;
        String root ="";
        if(Strings.isBlank(data)){
            type="html";
            data="\"hello,${name!} \\r\\n你好:${name!}\"";
        }
        if(Strings.isBlank(type)){
            type="html";
        }
        try {
            Configuration cfg =Configuration.defaultConfiguration();
            switch (type) {
                case "classpath":
                    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
                    gt = new GroupTemplate(resourceLoader, cfg);
                    break;
                case "user":
                    root = getPath(true);
                case "beetl":
                    FileResourceLoader fileLoader = new FileResourceLoader(BeetlUtil.getWebRoot(),"utf-8");
                    gt = new GroupTemplate(fileLoader, cfg);
                    break;
                case "html":
                    StringTemplateResourceLoader stringLoader = new StringTemplateResourceLoader();
                    gt = new GroupTemplate(stringLoader, cfg);
                    break;
            }
        } catch (IOException e) {
            System.out.println(e);
        }
        return gt.getTemplate(data);
    }

调用为:
Template template= Utils.getTemplate("/pages/user/company.html","beetl");

这个模板文件所在项目位置:webapp/WEB-INF/views/pages/user/company.html
webapp/WEB-INF/views/ 为beetl根目录。

非打包模式下运行正常无误。

打包后运行出现如下错误:

[DEBUG] 2018-05-10 22:21:28,133 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - SELECT * FROM sys_menu  WHERE href IN (?,?) ORDER BY href DESC, path DESC
    |         1 |                                            2 |
    |-----------|----------------------------------------------|
    | /template | /template/beetl?url=/pages/user/company.html |
  For example:> "SELECT * FROM sys_menu  WHERE href IN ('/template','/template/beetl?url=/pages/user/company.html') ORDER BY href DESC, path DESC  "
>>10:21:28:错找不到指定模板或者加载错(TEMPLATE_LOAD_ERROR): 位于0行 资源:/pages/user/company.html 模板根目录为 C:\working\project\shu.epei.xin\dst\tmp\webinf\WEB-INF\views
FileResourceLoader,Root=C:\working\project\shu.epei.xin\dst\tmp\webinf\WEB-INF\views
[DEBUG] 2018-05-10 22:21:28,322 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [GET ]URI=/epei/template/beetl 1902ms
22:21:28.559 [QuartzScheduler_defaultScheduler-DESKTOP-EVPFAKR1525962067926_ClusterManager] DEBUG org.quartz.impl.jdbcjobstore.JobStoreTX - ClusterManager: Check-in complete.
22:21:36.240 [defau

请指教!

25 回复

我在打包运行目录\tmp\webinf\WEB-INF下确实没有看到views目录

要用ClasspathResourceLoader

OK,我试试!

如果写死为classpath下运行,在打包模式下运行正常!
但是idea编译模式下运行又出现错误

[DEBUG] 2018-05-10 22:36:46,566 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - SELECT * FROM sys_menu  WHERE href IN (?,?) ORDER BY href DESC, path DESC  
    |         1 |                                                2 |
    |-----------|--------------------------------------------------|
    | /template | /template/classpath?url=/pages/user/company.html |
  For example:> "SELECT * FROM sys_menu  WHERE href IN ('/template','/template/classpath?url=/pages/user/company.html') ORDER BY href DESC, path DESC  "
>>10:36:46:错找不到指定模板或者加载错(TEMPLATE_LOAD_ERROR): 位于0行 资源:/pages/user/company.html
ClassLoader:WebAppClassLoader=NutzWk@103c97ff Path:/WEB-INF/views

是否需要判断一下,打包模式下用classpath找模板,编译模式下用beetl找模板?如何知道在打包模式下运行?

webapp/WEB-INF/views/pages/user/company.html 也在classpath里哦

当前调用代码是怎么写的?

Template template= Utils.getTemplate("webapp/WEB-INF/views/pages/user/company.html","classpath");

额, 你用的是nutzboot吗?

什么版本的nutzwk?

不是nutzboot是,nutzwk4.0

哦, 那用WebAppResourceLoader才行哦

入口:

 @At(value = {"/?/template/?/?/?","/?/template/?"},top = true)
    @Ok("raw:html")
    public String template(String appName,String type,String dataId,String id,@Param("url") String template,@Param("backto") String backto,  HttpServletRequest req) {
        NutMap map=menuService.setReq(req,appName,backto);
        return cmsPageService.getHtml(template,type,dataId,id,map);
    }

然后

public String getHtml(String url,String type,String dataId,String id,NutMap map){
        if(map==null)map=NutMap.NEW();
        if(Strings.isNotBlank(dataId)&&Strings.isNotBlank(id)) {
            Record data = dbmInterfaceService.load(currUser.authType(), dataId, NutMap.NEW().addv("id", id));
            map.put("obj", data);
        }
        Template template= Utils.getTemplate(url,type);
        return getHtml(template,map);
    }

WebAppResourceLoader能解决这个问题

好的,我再试试

话说, 为啥你不使用已有的ResourceLoader实例呢??

Setup类里面就能拿到

我觉得需要定位beetl下模板用他的资源类好点昵!唉,坑呀

那你走Setup拿到已有的ResourceLoader/GroupTemplate实例啦, 从BeetlViewMaker取

是通过如下引用拿这个实例吗?
@Inject
private GroupTemplate groupTemplate;

我在其他类里面引用出现错误:

Caused by: java.lang.RuntimeException: IocBean[cmsPageServiceImpl] fail at field=[groupTemplate]
	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)
	... 67 more
Caused by: org.nutz.ioc.IocException: IocBean[class:org.beetl.core.GroupTemplate] none ioc bean match class=org.beetl.core.GroupTemplate
	at org.nutz.ioc.impl.NutIoc.getByType(NutIoc.java:461)
	at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:66)
	at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
	... 69 more

要是在setup里面如何从BeetlViewMaker取,给段代码呗

目前其他途径都试过了,不能兼顾编译与打包模式,只有这最后一根稻草了!

cn.wizzer.app.web.commons.core.Setup类的init末尾下面的语句, 然后Globals.groupTemplate是新增的public static就行

	    // BeetlViewMaker要处理一下
            for (ViewMaker vm : nc.getViewMakers()) {
            if (vm instanceof BeetlViewMaker) {
                Globals.groupTemplate = ((BeetlViewMaker)vm).groupTemplate;
            }
        }

groupTemplate就跟@Ok里面beetl一个对象了

我的,我在测试,调试下OK!我现在打包试试

@wendal 非常感谢,完美解决!

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