如何在 Wicket 的 Ajax 响应中设置自定义 HTTP 响应标头?
我需要为 Wicket 应用程序的所有响应设置自定义 HTTP 标头。我目前正在自定义 RequestCycle 中执行此操作,其中 getWebResponse() 被按照以下方式重写:
@Override
public WebResponse getWebResponse() {
WebResponse response = super.getWebResponse();
response.setHeader("X-custom", "..." );
return response;
}
这非常有效,直到现在我已经切换到使用 AjaxCheckBox (类似的东西)而不是某些配置选项的普通复选框。
我的问题是,是否有一种简单的方法可以将我的自定义标头也包含在 Wicket 的 Ajax 响应中?
I need to set a custom HTTP header to all responses from my Wicket application. I'm currently doing it in a custom RequestCycle, where getWebResponse() is overridden along these lines:
@Override
public WebResponse getWebResponse() {
WebResponse response = super.getWebResponse();
response.setHeader("X-custom", "..." );
return response;
}
This has worked great, until now that I've switched to using AjaxCheckBox (something like this) instead of normal CheckBox for certain configuration options.
My question is, is there an easy way to include my custom header also in Wicket's Ajax responses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了办法。最后,实际上一点也不难。当使用调试器运行一些请求时,我注意到
onEndRequest()
确实也会被 Ajax 请求调用。onEndRequest()
方法已在我们的自定义 RequestCycle 实现中被重写用于其他目的(事务提交),因此我只是将设置标头的代码从getWebResponse()
移至此处。也许这里唯一不明显的事情是我需要将
response
转换为 WebResponse (当字段的类型为 响应),以便能够调用setHeader()
。这可以在 普通的Java EE过滤器也是如此,通过在
chain.doFilter()
调用之后设置标头(请参阅我对该问题的第二条评论)。我没有选择它,因为 1)我不清楚如何在那里连接数据访问,2)如果可以避免的话,我不想要额外的移动部件。我们已经将 RequestCycle 子类用于 HTTP 标头相关的事情,这非常适合。事实上,此更改简化了该类,因为没有理由再重写getWebResponse()
!I found a way. It actually wasn't hard at all, in the end. When running through some requests with my debugger, I noticed that
onEndRequest()
does get called for Ajax requests too.The
onEndRequest()
method was already overriden in our custom RequestCycle implementation for other purposes (transaction commit), so I just moved the code that sets the header there fromgetWebResponse()
.Perhaps the only non-obvious thing here was that I needed to cast
response
into WebResponse (when the field's type is Response) to be able to callsetHeader()
.This could have been done in a normal Java EE filter too, by setting the header after
chain.doFilter()
call (see my second comment on the question). I didn't choose that because 1) it wasn't clear to me how to wire up data access there and 2) I don't want extra moving parts if I can avoid it. We already use our RequestCycle subclass for HTTP header related things and this fits in nicely. In fact, this change simplified that class, as there's no reason to overridegetWebResponse()
anymore!在幕后,Wicket 仍然使用标准的 Java HTML 堆栈。因此,无需重写现有方法,只需实现 Filter 并将其注册到您的
web.xml
。使用正确的 URL 模式,它将适用于所有请求,无论由谁处理。Under the hood, Wicket still uses the standard Java HTML stack. So instead of overriding existing methods, just implement a Filter and register it in your
web.xml
. With the correct URL pattern, it will apply to all requests, no matter who handles them.查看 AjaxRequestTarget 的实现
[...]
[...]
Wicket 解决方案将是重写 RequestCycle.getResponse() 。
Looking at the implementation of AjaxRequestTarget
[...]
[...]
the Wicket solution would be to override RequestCycle.getResponse() instead.