Get request header from freemarker

不打扰是莪最后的温柔 提交于 2020-01-06 11:02:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!