简体   繁体   English

如何从Servlets访问@ViewScoped托管bean?

[英]How to access @ViewScoped managed beans from Servlets?

I would like to access a view scope managed bean inside a Servlet. 我想访问Servlet中的视图范围托管bean。

I searched for similar question and got these ones: 我搜索了类似的问题,得到了这些问题:

how to get beans from view scope which uses FacesContext.getCurrentInstance() , inapplicable inside a Servlet because it should give a null result 如何从使用FacesContext.getCurrentInstance() 视图范围中获取bean ,在Servlet中不适用,因为它应该给出null结果

JSF - get managed bean by name which treats all other scopes but not viewscope JSF - 按名称获取托管bean,它处理所有其他范围但不处理viewcope

I'm going to think it is not possible, is it ? 我会认为这是不可能的,不是吗?

There is the simple solution to change that bean scope to session scope but it is a last chance because I'm worried by memory consumption. 有一个简单的解决方案可以将bean范围更改为会话范围,但这是最后的机会,因为我担心内存消耗。

My final need is the following: the managebean contains some data displayed in a dataTable. 我最后需要的是:managebean包含dataTable中显示的一些数据。 The same page should include the image (mandatory) of chart representation of those data. 同一页面应包括这些数据的图表表示的图像(强制性)。 The image can be produced by a Servlet but it needs to access the data or reload them from the db. 图像可以由Servlet生成,但它需要访问数据或从db重新加载它们。

I also tried the <p:graphicImage> from PrimeFaces 2.X but it is not compatible with viewscope beans. 我也尝试过PrimeFaces 2.X中的<p:graphicImage>但它与viewscope bean不兼容。 Any suggestion? 有什么建议吗?

That's not possible, no. 那是不可能的,不。 The view scope is tied to a specific JSF view, which is no means of in a plain HTTP servlet request. 视图范围与特定的JSF视图相关联,这在普通的HTTP servlet请求中是无法实现的。 Note that you can't access concrete request scoped beans during a plain HTTP servlet request either, they would always return null . 请注意,在普通HTTP servlet请求期间,您无法访问具体的请求作用域bean,它们将始终返回null

The session scope is the best what you can get. 会话范围是您可以获得的最佳范围。 You can just remove the attribute from the session scope once you're finished with it in the servlet. 一旦在servlet中完成了属性,就可以从会话范围中删除该属性。

session.removeAttribute("somename");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM