JSP 中的 CDI 注入
在 JSP 中,可以使用 EL 表达式(例如 ${myBean.myAttribute})来使用 CDI 托管 Bean。这里没问题。
我想在 JSP 文件中使用“常规注入”(即不使用 EL 表达式)和 @Inject,例如: <%! @Inject MyBean myBean; %>然后稍后<%= myBean.getMyAttribute() %>。即使该示例可以使用 EL 表达式来实现,但其他一些用例却不能。
应用服务器似乎并不完全支持这一点:
- JBoss 6.0.0、JBoss 6.1.0、Resin 4.0.22:好的,工作得很好。
- JBoss 7.0.1、GlassFish 3.x(测试了多个版本):失败,myBean 仍然为空。
它应该在 JSP 中正常工作,因为:
(1) 根据各种相关规范,它在 servlet 中运行良好
(2) JSP 在运行时被转换为servlet。
你们知道我想做的事情是否得到支持吗?也许有任何内部/实施信息?
Within a JSP it is possible to use CDI managed beans using EL expressions such as ${myBean.myAttribute}. No problem here.
I would like to use "regular injection" (i.e. without using EL expressions) with @Inject in JSP files, for example :
<%! @Inject MyBean myBean; %> then later <%= myBean.getMyAttribute() %>. Even if that example can be achieved using EL expressions, some other use cases cannot.
This does not seem to be completely supported by app servers:
- JBoss 6.0.0, JBoss 6.1.0, Resin 4.0.22: OK, it works just fine.
- JBoss 7.0.1, GlassFish 3.x (several versions were tested): FAILS, myBean remains null.
It should work fine in a JSP since :
(1) it works fine in servlets according to the various concerned specifications and
(2) JSP get translated to servlet during runtime.
Do you guys know if what I'm trying to do is supported or not? Any inside/implementation information maybe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有趣的问题,如果您没有测试过它,我会赌一些钱,因为它不起作用;-)
CDI 构建在托管 bean (JSR 316) 上。相应的定义非常宽松(故意):
根据规范:
可能发生的情况是容器扫描类路径并碰巧找到已编译的 JSP servlet。自从我上次看到它以来已经有一段时间了,但我记得代码已生成,所有内容(包括 scriptlet)都落在
doGet()
或doPost()
中......! ?因此,尽管它们在定义上不取消资格,但我怀疑 JSP scriplet 是否属于托管 bean。老实说,这感觉非常错误;-)我很长时间以来一直在关注 CDI / Weld / Seam 邮件列表,并且不记得曾经提到过 JSP。与谷歌搜索此连接相同。
因此,您不应该依赖 CDI 来处理 scriptlet。恕我直言,这种行为比故意的行为有更多的副作用,并且可以在未来的版本中删除,恕不另行通知(甚至没有被注意到:-)
所以,+1对于JB Nizet的提议:使用servlet 具有 CDI,但不具有 JSP。
更新:我试图提供帮助,而不是制造混乱;-) 我的观点是:恕我直言,在 JSP 中使用 CDI 感觉真的非常错误,但我没有在相关规范中找到任何可以证明这一点的内容。我只能说 JSP 在任何地方都从未被提及 - 这支持了我的直觉(并且符合某些实现确实考虑它而其他实现则不考虑的观察结果)。
Interesting question, if you hadn't been testing it, I would have bet some money on the fact the it does not work ;-)
CDI builds on managed beans (JSR 316). The corresponding definition is pretty relaxed (on purpose):
From the spec:
What's probably happening is that the container scans the classpath and happens to find the compiled JSP servlets. It's a while since I last saw one, but I remember that the code is generated and everything (including scriptlets) lands in
doGet()
ordoPost()
...!? So, even though they formally do not disqualify in terms of the definition, I doubt that a JSP scriplet is anything that you want to consider a managed bean. It feels terribly wrong, honestly ;-)I'm following the CDI / Weld / Seam mailing lists since quite a while, and don't recall that JSP has ever been mentioned. Same with googling this connection.
As a consequence you should not rely on CDI working with scriptlets. IMHO this behaviour has more of a side effect than of something intenional and can be dropped in future releases without notice (or even without being noticed :-)
So, +1 for the proposal of JB Nizet: Use servlets with CDI, but not JSPs.
UPDATE: I tried to help, not to create confusion ;-) My point is: IMHO it feels really really wrong to use CDI in JSPs, but I did not find anything in the relevant specs that proves this. All I can say is that JSPs are never mentioned anywhere - which kind of supports my gut feeling (and fits the observation that some implementations do consider it, others don't).
我认为 JSP 没有现成可用的可移植 @Inject,但应该可以像使用 servlet 一样实现它(在容器级别)。
虽然我同意这不是利用 CDI 的最佳方式,但从技术上讲,我没有看到任何反对它的理由。例如,据我所知,Servlet 中的 @Inject 透明地使用 ThreadLocal 代理,为什么不在 JSP 中使用此功能呢?
I don't think there is a portable @Inject available out-of-box for JSP, but it should be possible to implement it (at the container level) the same way it works with servlets.
And while I agree that's it not the best way to utilize CDI, technically I don't see any reason against it. For example, AFAIK @Inject in servlets transparently uses ThreadLocal proxies, why not use this feature in JSP's?
试试这个:-
它对我有用:)
注意: 使用括号
<%! %>
而不是<% %>
。Try this :-
It's working for me :)
Note: Use bracket
<%! %>
instead of<% %>
.