在 servlet 和 java 文件之间传递值
我有一个 spring 控制器,它设置一些要请求的值并显示一个 jsp 页面。对于视图部分,我们使用图块。结果页面有 3 个部分, header 、 content 和 footer jsp 。 这个头jsp使用一个java文件,我想从这个文件访问第一个spring控制器创建的属性。有没有什么方法可以在不使用会话的情况下做到这一点? 当我尝试 request.getAttribute 时,它给出 null。我认为这是因为它不是请求值设置后的立即文件。
I've one spring controller which is setting some values to request and shows a jsp page. For the view part we use tiles. The result page has 3 parts, header , content and footer jsp's.
This header jsp use a java file and i want to access the attributes created by the first spring controller from this file. Is there any way to do that without using session?
When I tried request.getAttribute, it gives null. I think it's because it's not an immediate file after the request values setting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要所有内容都在同一个请求中运行,并且控制器代码在视图部分之前执行,
setAttribute()
就应该可以工作。要调试此类问题,请使用Filter
将请求 URL 和属性转储到控制台或日志。如果这些调用位于不同的请求中,您有两个选择:会话和 Spring bean(使用会话 bean 或您自己的实现)。我更喜欢 bean,因为它们是类型安全的,并且它们允许我将代码与测试复杂的 Servlet API 分开。
As long as everything runs in the same request and the controller code is executed before the view part,
setAttribute()
should work. To debug issues like that, use aFilter
which dumps the request URL and attributes to the console or the log.If those calls are in different requests, you have two options: The session and a Spring bean (use a session bean or your own implementation). I prefer beans since they are type safe and they allow me to separate my code from the Servlet API which is complex to test.
您确实需要输入一些代码才能获得代码答案,但除非您使用 JSP scriptlet,否则我猜测这是您在标头中使用的 Java bean。这当然无法访问请求(因此是会话),也不应该真正访问。如果您希望它能够访问请求/会话,您可能想要做的是将其转换为标签库。
You'll really need to put some code to get a code answer but unless you're using JSP scriptlets I'm guessing this is a Java bean that you're using in the header. This of course cannot access the request (hence the session) nor should it really. What you probably want to do is convert it to a tag library if you want it to have access to the request/session.