开发者

FreeMarker - Get Current URL

开发者 https://www.devze.com 2023-01-06 17:01 出处:网络
开发者_StackOverflow社区Is it possible to get the current page\'s URL in FTL?As far as I can tell, freemarker is strictly a templating engine -- it simply produces text, and has no way of knowing wher
开发者_StackOverflow社区

Is it possible to get the current page's URL in FTL?


As far as I can tell, freemarker is strictly a templating engine -- it simply produces text, and has no way of knowing where that text will appear. If you want to include the "current page's URL", you'll either have to pass that data into the template from the host Java code (recommended) or you'll have to detect it client-side using javascript.


I'm running Spring 3.2.x and exposeSpringMacroHelpers defaults to true.

As per Spring Documentation

Set whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext". Default is "true".

On my view, I can then do something like

<#if springMacroRequestContext.requestUri?contains("/login")>

Hope it helps.


Just as a supplement: If you are using the FreemarkerServlet, a hash named 'Request' is passed to every template which should have one key requestURL according to the documentation here.


Yes, it's possible. Make sure you add "exposeSpringMacroHelpers" for your view resolver in your application context.

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
    <property name="exposeSpringMacroHelpers" value="true" />
    <property name="prefix" value="/view/" />
    <property name="suffix" value=".ftl" />
</bean>

Then call ${springMacroRequestContext.getRequestUri()} in your template

<span>This page's url is: ${springMacroRequestContext.getRequestUri()}</span>


Just use this line:

<#assign url = http.request.url />
${url}


[#assign currentSiteName = sitefn.site().name]
${currentSiteName}${state.currentURI}

currentSiteName will give the home page url and state.currentURI will give the later part, so you can form it as:

baseURL + ${currentSiteName}${state.currentURI}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号