如何在 Wicket 的 Ajax 响应中设置自定义 HTTP 响应标头?

发布于 2024-10-06 18:56:35 字数 594 浏览 0 评论 0原文

我需要为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初吻给了烟 2024-10-13 18:56:35

我找到了办法。最后,实际上一点也不难。当使用调试器运行一些请求时,我注意到 onEndRequest() 确实也会被 Ajax 请求调用

onEndRequest() 方法已在我们的自定义 RequestCycle 实现中被重写用于其他目的(事务提交),因此我只是将设置标头的代码从 getWebResponse() 移至此处。

@Override
protected void onEndRequest() {
    super.onEndRequest();
    ((WebResponse) response).setHeader("X-custom", "..." );
    // ...
}

也许这里唯一不明显的事情是我需要将 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 from getWebResponse().

@Override
protected void onEndRequest() {
    super.onEndRequest();
    ((WebResponse) response).setHeader("X-custom", "..." );
    // ...
}

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 call setHeader().

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 override getWebResponse() anymore!

爱本泡沫多脆弱 2024-10-13 18:56:35

在幕后,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.

难理解 2024-10-13 18:56:35

查看 AjaxRequestTarget 的实现

[...]

/**
 * @see org.apache.wicket.IRequestTarget#respond(org.apache.wicket.RequestCycle)
 */
public final void respond(final RequestCycle requestCycle)
{
    final WebResponse response = (WebResponse)requestCycle.getResponse();

    if (markupIdToComponent.values().contains(page))

[...]

Wicket 解决方案将是重写 RequestCycle.getResponse() 。

Looking at the implementation of AjaxRequestTarget

[...]

/**
 * @see org.apache.wicket.IRequestTarget#respond(org.apache.wicket.RequestCycle)
 */
public final void respond(final RequestCycle requestCycle)
{
    final WebResponse response = (WebResponse)requestCycle.getResponse();

    if (markupIdToComponent.values().contains(page))

[...]

the Wicket solution would be to override RequestCycle.getResponse() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文