小白提问:jFinal + beetl 在 Jboss 7 下,怎么获取模板路径?

byrdkm17 发布于 2013/05/21 16:55
阅读 1K+
收藏 0

JBoss 版本是 7.1.1

在 jFinal 中这样加载 beetl 后

public void configConstant(Constants me) {
        me.setMainRenderFactory(new BeetlRenderFactory());
 }
 发现指定的模板路径变为 %JBOSS_HOME%/modules/sun/jdk/main/service-loader-resources

请问要如何才能正确获取到视图所在路径?

加载中
0
byrdkm17
byrdkm17
使用 jfinal 自带的 PathKit 来获取 webRootPath 就能正确获取到模版路径了
0
闲大赋
闲大赋
import java.io.IOException;

import org.bee.tl.core.GroupTemplate;
import org.bee.tl.ext.WebConfig;
import org.bee.tl.ext.WebPathKit;

import com.jfinal.render.IMainRenderFactory;
import com.jfinal.render.Render;

public class BeetlRenderFactory implements IMainRenderFactory
{
	public static WebConfig config = null;
	public static String viewExtension = ".html";
	public static GroupTemplate groupTemplate = null;

	static
	{
		try
		{
			config = new WebConfig();
			config.updateRootPath(WebPathKit.getWebRootPath()); //更新模版路径
			groupTemplate = config.createGroupTemplate();
		}
		catch (IOException e)
		{
			throw new RuntimeException("Can notload properties for beetl");
		}
	}

	public Render getRender(String view)
	{
		return new BeetlRender(groupTemplate, view);
	}

	
	public String getViewExtension()
	{
		return viewExtension;
	}

}

config.updateRootPath(你的模板路径);

 你说的模板路径变成main/service-loader-resources..这不是你期望的目录,那期望的是在哪儿?

0
byrdkm17
byrdkm17

引用来自“闲.大赋”的答案

import java.io.IOException;

import org.bee.tl.core.GroupTemplate;
import org.bee.tl.ext.WebConfig;
import org.bee.tl.ext.WebPathKit;

import com.jfinal.render.IMainRenderFactory;
import com.jfinal.render.Render;

public class BeetlRenderFactory implements IMainRenderFactory
{
	public static WebConfig config = null;
	public static String viewExtension = ".html";
	public static GroupTemplate groupTemplate = null;

	static
	{
		try
		{
			config = new WebConfig();
			config.updateRootPath(WebPathKit.getWebRootPath()); //更新模版路径
			groupTemplate = config.createGroupTemplate();
		}
		catch (IOException e)
		{
			throw new RuntimeException("Can notload properties for beetl");
		}
	}

	public Render getRender(String view)
	{
		return new BeetlRender(groupTemplate, view);
	}

	
	public String getViewExtension()
	{
		return viewExtension;
	}

}

config.updateRootPath(你的模板路径);

 你说的模板路径变成main/service-loader-resources..这不是你期望的目录,那期望的是在哪儿?

我的模板视图是放在 /WEB-INF/template/ 下, 如 /WEB-INF/template/index.html

现在得到的地址是 %JBOSS_HOME%/modules/sun/jdk/WEB-INF/template/index.html

0
闲大赋
闲大赋
这是对的啊
0
byrdkm17
byrdkm17
22:06:56,647 ERROR [com.jfinal.core.ActionHandler] (http--127.0.0.1-8080-1) /: com.jfinal.render.RenderException: java.io.FileNotFoundException: D:\Eclipse\JBossApplicationServer\jboss-as-7.1.1.Final\modules\sun\jdk\WEB-INF\template\index.html (系统找不到指定的路径。)
	at org.bee.tl.ext.jfinal.BeetlRender.render(BeetlRender.java:98) [bee-1.22.jar:]
	at com.jfinal.core.ActionHandler.handle(ActionHandler.java:92) [jfinal-bin-1.4-with-src.jar:]
	at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72) [jfinal-bin-1.4-with-src.jar:]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
	at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
	at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_41]
Caused by: java.io.FileNotFoundException: D:\Eclipse\JBossApplicationServer\jboss-as-7.1.1.Final\modules\sun\jdk\WEB-INF\template\index.html (系统找不到指定的路径。)
	at java.io.FileInputStream.open(Native Method) [rt.jar:1.6.0_41]
	at java.io.FileInputStream.<init>(FileInputStream.java:120) [rt.jar:1.6.0_41]
	at org.bee.tl.core.Resource.getReader(Resource.java:127) [bee-1.22.jar:]
	at org.bee.tl.core.GroupTemplate.getFileTemplate(GroupTemplate.java:443) [bee-1.22.jar:]
	at org.bee.tl.ext.jfinal.BeetlRender.render(BeetlRender.java:62) [bee-1.22.jar:]
	... 15 more

这个是运行后的报错,我的项目结构如下:

-------- src
| | ---- main
| | | ---- java
| | | ---- resources
| | | ---- webapp
| | | | ---- WEB-INF
| | | | | ---- template
| | | | | | ---- index.html
| | | | | ----web.xml

返回顶部
顶部