在SpringMVC框架中使用Freemarker试图时,要获取根路径的方式有两种:
第一种:
继承FreeMarkerView
public class BaseFreeMarkerView extends FreeMarkerView {
private static final String CONTEXT_PATH = "base";
@Override
protected void exposeHelpers(Map<String, Object> model,
HttpServletRequest request) throws Exception {
model.put(CONTEXT_PATH, request.getContextPath());
super.exposeHelpers(model, request);
}
}
通过重写exposeHelpers方法,在spring里配置自己的freemarker的视图解析器,在模板中就可以通过${base}获取。
第二种:
<
bean
class
=
"org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"
p:prefix
=
"/"
p:suffix
=
".ftl"
>
<
property
name
=
"viewClass"
value
=
"org.springframework.web.servlet.view.freemarker.FreeMarkerView"
/>
<
property
name
=
"contentType"
value
=
"text/html;charset=UTF-8"
></
property
>
<
property
name
=
"requestContextAttribute"
value
=
"base"
/>
<
property
name
=
"order"
value
=
"0"
></
property
>
</
bean
>
|
起作用的是那一句?
1
|
<
property
name
=
"requestContextAttribute"
value
=
"base"
/>
|
在页面使用方式
1
|
<
link
type
=
"favicon"
rel
=
"shortcut icon"
href
=
"${base.contextPath}/favicon.ico"
|