在任何 Servlet 相关类中按名称获取 JSF 托管 bean
我正在尝试编写一个自定义 servlet(用于 AJAX/JSON),我想在其中按名称引用我的 @ManagedBeans
。我希望将:
http://host/app/myBean/myProperty
映射到:
@ManagedBean(name="myBean")
public class MyBean {
public String getMyProperty();
}
是否可以从常规 servlet 按名称加载 bean?是否有我可以使用的 JSF servlet 或帮助程序?
我似乎被春天宠坏了,这一切都太明显了。
I'm trying to write a custom servlet (for AJAX/JSON) in which I would like to reference my @ManagedBeans
by name. I'm hoping to map:
http://host/app/myBean/myProperty
to:
@ManagedBean(name="myBean")
public class MyBean {
public String getMyProperty();
}
Is it possible to load a bean by name from a regular servlet? Is there a JSF servlet or helper I could use for it?
I seem to be spoilt by Spring in which all this is too obvious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在基于 servlet 的工件中,例如
@WebServlet
、@WebFilter
和@WebListener
,您可以获取“普通”JSF@ManagedBean @RequestScoped
by:和
@ManagedBean @SessionScoped
by:和
@ManagedBean @ApplicationScoped
by:请注意,这先决条件是该 bean 已由 JSF 自动创建预先。否则这些将返回
null
。然后,您需要手动创建 bean 并使用setAttribute("beanName", bean)
。如果您能够使用 CDI
@Named
而不是自 JSF 2.3 以来已弃用的
@ManagedBean
,那么它就更容易了,特别是因为您不再需要手动创建 bean:请注意,这不起作用当您使用
@Named @ViewScoped
时,因为该 bean 只能由 JSF 视图状态标识,并且仅在调用FacesServlet
时才可用。因此,在之前运行的过滤器中,访问@Inject
ed@ViewScoped
将始终抛出ContextNotActiveException
。仅当您位于
@ManagedBean
内时,您才能使用@ManagedProperty
:请注意,这在
@Named
或@WebServlet
内不起作用,或者任何其他工件。它确实仅在@ManagedBean
内部有效。如果您不在
@ManagedBean
内,但FacesContext
随时可用(即FacesContext#getCurrentInstance()
不会返回null
),您还可以使用Application#evaluateExpressionGet()
:可以方便如下:
并且可以使用如下所示:
另请参阅:
In a servlet based artifact, such as
@WebServlet
,@WebFilter
and@WebListener
, you can grab a "plain vanilla" JSF@ManagedBean @RequestScoped
by:and
@ManagedBean @SessionScoped
by:and
@ManagedBean @ApplicationScoped
by:Note that this prerequires that the bean is already autocreated by JSF beforehand. Else these will return
null
. You'd then need to manually create the bean and usesetAttribute("beanName", bean)
.If you're able to use CDI
@Named
instead of the since JSF 2.3 deprecated@ManagedBean
, then it's even more easy, particularly because you don't anymore need to manually create the beans:Note that this won't work when you're using
@Named @ViewScoped
because the bean can only be identified by JSF view state and that's only available when theFacesServlet
has been invoked. So in a filter which runs before that, accessing an@Inject
ed@ViewScoped
will always throwContextNotActiveException
.Only when you're inside
@ManagedBean
, then you can use@ManagedProperty
:Note that this doesn't work inside a
@Named
or@WebServlet
or any other artifact. It really works inside@ManagedBean
only.If you're not inside a
@ManagedBean
, but theFacesContext
is readily available (i.e.FacesContext#getCurrentInstance()
doesn't returnnull
), you can also useApplication#evaluateExpressionGet()
:which can be convenienced as follows:
and can be used as follows:
See also:
我使用以下方法:
这允许我以类型化的方式获取返回的对象。
I use the following method:
This allows me to get the returned object in a typed manner.
您是否尝试过类似此链接的方法?我不确定
createValueBinding()
是否仍然可用,但这样的代码应该可以从普通的旧 Servlet 访问。这确实需要 bean 已经存在。http://www.coderanch.com/ t/211706/JSF/java/access-management-bean-JSF-from
Have you tried an approach like on this link? I'm not sure if
createValueBinding()
is still available but code like this should be accessible from a plain old Servlet. This does require to bean to already exist.http://www.coderanch.com/t/211706/JSF/java/access-managed-bean-JSF-from
您可以通过传递名称来获取托管 bean:
You can get the managed bean by passing the name:
我有同样的要求。
我已经使用下面的方式来获取它。
我有会话范围的bean。
我在 servlet doPost() 方法中使用了以下代码。
它解决了我的问题。
I had same requirement.
I have used the below way to get it.
I had session scoped bean.
I have used the below code in my servlet doPost() method.
it solved my problem.
我使用这个:
然后调用:
这样您就可以重构代码并毫无问题地跟踪使用情况。
I use this:
And then call:
This way you can refactor your code and track usages without problems.