日期:2014-05-16  浏览次数:20407 次

请问在FreeMarker中如何引用JSTL标签
   JSTL的<c:url="ddd"/>标签可以得到应用程序的部署目录,但是FreeMarker中如何知识应用程序的部署目录呢?在Google和Baidu以及各大论坛都狂搜索了一气,可以还是没有结果。
    http://smile6688.iteye.com/blog/49642 说明了FreeMarker引用Struts标签的方法,不知道FreeMarker能否引用JSTL的标签,如何引用呢?或者有没有什么办法可以在FreeMarker中使用类似于JSTL的<c:url="ddd"/>功能。
   非常感谢!
1 楼 quickselect 2007-05-29  
这个问题我也头疼啊,期望哪位用过的指点一下。或者说不能用??
2 楼 hajunma 2007-05-29  
    FreeMarker本来和JSTL就是两个东西,就象Struts和Tapestry一样,我建议你直接用JSTL,为什么要在FreeMarker中用JSTL呢?我怀疑这个比较难啊。
3 楼 ahuaxuan 2007-05-29  
没有必要为这个功能在模板导入标签
request.getSession().getServletContext().getRealPath("");
使用这个方法就能得到工程部署的决定路径,如果你是想得到contextpath,就用这个方法request.getContextPath()
4 楼 codeutil 2007-05-29  

网上有引用jstl的例子:
http://www.blogjava.net/vip01/archive/2007/05/10/116431.html

5 楼 stamen 2007-05-29  
codeutil 写道

网上有引用jstl的例子:
http://www.blogjava.net/vip01/archive/2007/05/10/116431.html


这个我试了,不行吧。
6 楼 stamen 2007-05-29  
  我已经解决了,谢谢大家。
解决办法是使用Spring为FreeMarker提供的宏:
1.通过exposeSpringMacroHelpers属性暴露Spring宏
<bean
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
	<property name="suffix" value=".ftl" />
	<property name="exposeRequestAttributes" value="true" />
	<property name="exposeSessionAttributes" value="true" />
	<property name="contentType" value="text/html; charset=utf-8"/>
	<property name="exposeSpringMacroHelpers" value="true"/><--注意这里
  </bean>


2.在模型文件中,通过<@spring.url 需要转换为URL/>,这相当于JSTL的<c:url value="ddd"/>
<#import "spring.ftl" as spring />  <---引入宏
  <html>
	<head>
		<title>
		  baobaotao
		</title>
	</head>
	<body>
		精华文章
		<table>
		    <#list topicList as topic>
				<tr>
					<td>
						${topic.topicId}
					</td>
					<td>					   
 <a href="[b]<@spring.url '/showTopic.html?topicId=${topic.topicId}'/>[/b]">${topic.title}</a>
					</td>
					<td>
					   ${topic.createDate?string("yyyy-MM-dd HH:mm:ss")}
					</td>
				</tr>
			</#list>
		 <table>
	</body>
</html>


 
7 楼 vip01 2007-05-30  
汗我失败的例子又被拿出来示众
8 楼 icefire 2007-05-30  
request.getSession().getServletContext().getRealPath("");
request.getContextPath();
这两个方法都不够用??
汗一个!
9 楼 totobacoo 2007-07-09  

Freemarker 本身是可以设置全局共享变量的。一旦设置了 SharedVariable,在当前 freemarkerManager 内是全局有效的。

你这个问题我是这样解决的,在容器 context 初始化的时候,读入域名的配置,设置到指定freemarker全局变量内。这样当前环境内,所有 ftl 都能够自由访问到。

 
public class SystemInitServlet extends HttpServlet {
  //....

  public void init() throws ServletException {