0 0

用freemarker后引入CSS和JS的路径问题3

寒假没事,准备做一个Demo型的C2C网站练练手,就模仿淘宝,现在知道了Freemarker模板技术,就想试试用它来生成商品详细信息的静态页面,发现在用Freemarker之后引入到文件中的CSS和JS文件都没有起作用,在JSP页面中还可以用<base href="<%=basePath%>">来设置绝对路径,请问怎么样在ftl文件中设置<base>标签的值?还有,生成静态页面之后怎么样显示用户登陆了的信息?

问题补充:
skzr.org 写道
全部采用绝对定位即可,比如我的使用,在一个全局macro定义中定义变量basePath即可
<#-- 取得 应用的绝对根路径 -->
<#assign basePath=request.contextPath>
<#macro head>
		<title>预祝成功</title>
		<link rel="shortcut icon" href="${basePath}/favicon.ico">
		<link rel="icon" type="image/gif" href="${basePath}/animated_favicon.gif">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
</#macro>

如果是在<body>中还需要这个绝对路径那应该怎么办呢?我现在的想法是,在省城HTML的Action中直接用HttpServletRequest获取到绝对路径path,放到root中,然后在ftl页面中使用<base href="path">

问题补充:
skzr.org 写道
<#import "/common/app.ftl" as app>
<base href="${app.basePath}/">

这是神马意思?common/app.ftl文件中是什么内容?
2011年2月20日 20:35

4个答案 按时间排序 按投票排序

0 0

采纳的答案

补充下,我这里的是spring mvc的,如果是struts2的好像内置basePath
直接调用${basePath}即可

2011年2月22日 09:07
0 0

首先定义公共宏文件app.ftl

<#-- 取得 应用的绝对根路径 -->
<#assign basePath=request.contextPath>
<#macro head>
		<title>预祝成功</title>
		<link rel="shortcut icon" href="${basePath}/favicon.ico">
		<link rel="icon" type="image/gif" href="${basePath}/animated_favicon.gif">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
</#macro>

所以在其他ftl文件中可以使用他了,比如在home.ftl中:
<#import "/common/app.ftl" as app> 
<base href="${app.basePath}/">

2011年2月21日 16:56
0 0

<#import "/common/app.ftl" as app>
<base href="${app.basePath}/">

2011年2月21日 10:10
0 0

全部采用绝对定位即可,比如我的使用,在一个全局macro定义中定义变量basePath即可

<#-- 取得 应用的绝对根路径 -->
<#assign basePath=request.contextPath>
<#macro head>
		<title>预祝成功</title>
		<link rel="shortcut icon" href="${basePath}/favicon.ico">
		<link rel="icon" type="image/gif" href="${basePath}/animated_favicon.gif">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
</#macro>

2011年2月21日 09:01

相关推荐

Global site tag (gtag.js) - Google Analytics