问题
I want to create AJAX-navigation on my site with Freemarker
as template engine.
If page are requested with XMLHttpRequest
, there is no need to include page header and footer. Code will be look like this:
[#if !XMLHttpRequest]
[#include "header.ftl"]
[/#if]
${content}
[#if !XMLHttpRequest]
[#include "footer.ftl"]
[/#if]
My question is how to define that the request came with AJAX. Client adds header X-Requested-With: XMLHttpRequest, and how do I get it in Freemarker? I tried to find it in HttpRequestHashModel:
[#assign XMLHttpRequest = Request.headers['X-Requested-With']=="XMLHttpRequest" /]
but it throws error Expression Request.headers is undefined
. I also tried to use RequestParameters it can't help too.
回答1:
FreeMarker itself doesn't define any HTTP related variables; it's not like JSP, it's a general purpose engine; it only sees variables that were passed to it, and it doesn't know what they are... they are just name-value pairs as far as FreeMarker is concerned. So if you need this information, then either you should pass it to FreeMarker in the actions (maybe globally with a filter or interceptor or whatever you have), or the web application framework should.
来源:https://stackoverflow.com/questions/17648120/get-request-header-from-freemarker