freemaker里如何获得路径Path

RobinsonLiao 发布于 2013/09/06 15:52
阅读 4K+
收藏 0
在jsp页面里我们通常获取路径方式是
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";


通过这种方式来方便的导入js和css

那么freemaker里面有没有对应的方式?在ftl页面里你怎么写导入css和js的路径

加载中
0
sjfengOS
sjfengOS

package com.yappam.website.activity.Interator;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class ResourcesHandlerInter extends HandlerInterceptorAdapter {


private String css;
private String js;
private String images;
private String webpath;
public String getWebpath() {
return webpath;
}
public void setWebpath(String webpath) {
this.webpath = webpath;
}
public String getCss() {
return css;
}
public void setCss(String css) {
this.css = css;
}
public String getJs() {
return js;
}
public void setJs(String js) {
this.js = js; 
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
request.setAttribute("css", css);
request.setAttribute("js", js);
request.setAttribute("images", images);
request.setAttribute("webpath", webpath);
return super.preHandle(request, response, handler);
}

}


这样在界面直接

<link rel="stylesheet" type="text/css" href="${css}/base.css">
<script type="text/javascript" src="${js}/jquery.js"></script>
<script type="text/javascript" src="${js}/jquery.metadata.js"></script>
<script type="text/javascript" src="${js}/jquery.validate.js"></script>


0
RobinsonLiao
RobinsonLiao

引用来自“sjfengOS”的答案

package com.yappam.website.activity.Interator;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class ResourcesHandlerInter extends HandlerInterceptorAdapter {


private String css;
private String js;
private String images;
private String webpath;
public String getWebpath() {
return webpath;
}
public void setWebpath(String webpath) {
this.webpath = webpath;
}
public String getCss() {
return css;
}
public void setCss(String css) {
this.css = css;
}
public String getJs() {
return js;
}
public void setJs(String js) {
this.js = js; 
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
request.setAttribute("css", css);
request.setAttribute("js", js);
request.setAttribute("images", images);
request.setAttribute("webpath", webpath);
return super.preHandle(request, response, handler);
}

}


这样在界面直接

<link rel="stylesheet" type="text/css" href="${css}/base.css">
<script type="text/javascript" src="${js}/jquery.js"></script>
<script type="text/javascript" src="${js}/jquery.metadata.js"></script>
<script type="text/javascript" src="${js}/jquery.validate.js"></script>


我用的也是spring mvc,后台生成路径不是很习惯
你这是用拦截器?
0
子木007
子木007
那要看你和什么框架结合了,  和 struts2 结合,默认设置 ${base} 为 contextpath.  没有的话就自己放一个就是了
0
RobinsonLiao
RobinsonLiao

引用来自“坑主”的答案

那要看你和什么框架结合了,  和 struts2 结合,默认设置 ${base} 为 contextpath.  没有的话就自己放一个就是了
前面是freemaker,后面是spring mvc的 controller。
0
RobinsonLiao
RobinsonLiao
request.setAttribute("webpath", webpath);  变量赋值的地方在哪呢???
0
Spance
Spance
相对路径   ../../abc/xxx
返回顶部
顶部