在SpringMVC框架中使用Freemarker视图时,要获取根路径的方式如下:
< bean id = " viewResolverFtl"
class = " org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" >
< property name = " viewClass"
value = " org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
< property name = " suffix" value = " .ftl" />
< property name = " contentType" value = " text/html;charset=UTF-8" />
< property name = " exposeRequestAttributes" value = " true" />
< property name = " exposeSessionAttributes" value = " true" />
< property name = " exposeSpringMacroHelpers" value = " true" />
< property name = " requestContextAttribute" value = " request" />
< property name = " cache" value = " true" />
< property name = " order" value = " 0" />
</ bean>
ftl
中的页面设置如下:
< #assign ctx = request.contextPath />
<!DOCTYPE html>
< html lang = " zh" >
< head>
< base id = " ctx" href = " ${ctx}" >
< title> 首页</ title>
< meta http-equiv = " Content-Type" content = " text/html; charset=UTF-8" >
< link href = " ${ctx}/static/bootstrap-3.3.4/css/bootstrap.min.css" rel = " stylesheet" >
< script src = " ${ctx}/static/bootstrap-3.3.4/js/bootstrap.min.js" > </ script>
js文件中获取path
var base = document. getElementById ( "ctx" ) . href;
$. ajax ( {
url : base + '/' + url,
data : value,
dataType : 'json' ,
type : 'post' ,
success : function ( data) {
success ( data) ;
} ,
error : function ( data) {
error ( data) ;
}
} ) ;