使用 Spring MVC 更改包含的 JSP 中的标头

发布于 2024-12-06 22:39:48 字数 536 浏览 1 评论 0原文

这对我来说非常有趣,但我有简单的 Spring MVC 应用程序和 JSP 页面。在包含的 Jsp 页面中,我想向我的应用程序添加一个 cookie。然而,尽管设置了它,但它无法在运行时解决。

这是我的 include jsp 页面上的代码。

   <% response.addCookie(new Cookie("test3","test3")); %>

我更喜欢在 jsp 级别编写应用程序的某些部分,而不是在控制器上编写。

我只能说,当我调用我的方法时,我正在使用 Tuckey UrlRewrite 和 at 而不是我的 jsp 页面,它工作正常。在我调用的方法中,我可以看到 MVC 控制器中的初始响应对象被另一个 HttpServletResponse 对象包装。好像转发到jsp后headers和cookie就不能改了?

有什么帮助吗?

PS:我已经更新了我的问题,以明确它是包含jsp的页面。

It is very interesting for me but I have simple Spring MVC application and JSP page. At Jsp pages which are included I would like to add a cookie to my application. However despite of setting it, It could not resolved at runtime.

this is the code at my included jsp page.

   <% response.addCookie(new Cookie("test3","test3")); %>

I prefer writing some of the parts of the our application at jsp level over writing at controller.

What I can just say is that I am using Tuckey UrlRewrite and at instead of my jsp pages when I call my method, it is working fine. And at my called method I can see that the inital response object at my MVC controller is wrapped by another HttpServletResponse object. It seems that headers and cookies could not be changed after forwarded to jsp?

Any help?

PS: I have updated my question to make it clear regarding it is jsp included page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

七秒鱼° 2024-12-13 22:39:48

JSP 是响应的一部分。您需要确保在提交响应之前执行该行。否则,您最终会在服务器日志中看到 IllegalStateException:响应已提交。因此,在将任何 HTML 发送到响应之前,将其放在 JSP 页面的最顶部。或者,更好的方法是,在转发到 JSP 之前将其放入 Spring 控制器、servlet 或过滤器中。

您还需要确保没有更改 包含的 JSP 文件内的响应。它将被简单地忽略。另请参阅 RequestDispatcher#include() javadoc:

ServletResponse 对象的路径元素和参数与调用者的保持不变。包含的 Servlet 无法更改响应状态代码或设置标头; 任何更改尝试都会被忽略

JSP is part of the response. You need to ensure that that line is exeucted before the response is been committed. Otherwise you end up with IllegalStateException: response already committed in the server logs. So put it in the very top of the JSP page, before any HTML is been sent to the response. Or, better, just put it in a Spring controller or a servlet or filter, far before the forward to JSP takes place.

You also need to ensure that you aren't altering the response inside a JSP file which is included by <jsp:include>. It will be plain ignored. See also the RequestDispatcher#include() javadoc:

The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.

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