从freemarker获取请求标头 [英] Get request header from freemarker

查看:113
本文介绍了从freemarker获取请求标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Freemarker作为模板引擎在我的网站上创建AJAX导航. 如果使用XMLHttpRequest请求页面,则无需包括页面页眉和页脚.代码将如下所示:

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]

我的问题是如何定义AJAX附带的请求.客户端添加标头X-Requested-With:XMLHttpRequest,如何在Freemarker中获得标头? 我试图在 HttpRequestHashModel

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" /]

,但会引发错误Expression Request.headers is undefined.我也尝试使用RequestParameters,它也无济于事.

but it throws error Expression Request.headers is undefined. I also tried to use RequestParameters it can't help too.

推荐答案

FreeMarker本身未定义任何与HTTP相关的变量.它不像JSP,而是通用引擎.它只看到传递给它的变量,并且不知道它们是什么...就FreeMarker而言,它们只是名称-值对.因此,如果您需要此信息,则应该在操作中将其传递给FreeMarker(可能是全局使用过滤器或拦截器,也可以是其他任何东西),或者Web应用程序框架应该将其传递.

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.

这篇关于从freemarker获取请求标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆