问题
Is it possible to get the current page's URL in FTL?
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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>
回答5:
Just use this line:
<#assign url = http.request.url />
${url}
来源:https://stackoverflow.com/questions/3207429/freemarker-get-current-url