如何从 Vaadin 7 应用程序中访问 `ServletContext`? [英] How to access `ServletContext` from within a Vaadin 7 app?
问题描述
如何访问当前的ServletContext
来自我的 Vaadin 7 应用程序?
How do I access the current ServletContext
from within my Vaadin 7 app?
我想使用 ServletContext
对象的 setAttribute
, getAttribute
, removeAttribute
和 getAttributeNames
为我的 Vaadin 应用程序管理一些全局状态的方法.
I want to use the ServletContext
object’s setAttribute
, getAttribute
, removeAttribute
, and getAttributeNames
methods to manage some global state for my Vaadin app.
此外,如果为此目的使用这些方法不适合 Vaadin 应用程序,请解释.
Also, if using those methods for that purpose is inappropriate for Vaadin apps, please explain.
推荐答案
tl;dr
对于 Vaadin 7 &8,以及 Vaadin Flow(版本 10+):
tl;dr
For Vaadin 7 & 8, as well as Vaadin Flow (versions 10+):
VaadinServlet.getCurrent().getServletContext()
VaadinServlet
VaadinServlet
类继承getServletContext
方法.
要获取 VaadinServlet
对象,请调用静态类方法 getCurrent
.
To get the VaadinServlet
object, call the static class method getCurrent
.
在您的 Vaadin 应用程序中的大部分位置,执行以下操作:
From most anywhere within your Vaadin app, do something like this:
ServletContext servletContext = VaadinServlet.getCurrent().getServletContext();
CAVEAT
在后台线程中不起作用.在您启动的线程中,此命令返回 NULL
.如文档所示:
在其他情况下(例如从以其他方式启动的后台线程),当前 servlet 不会自动定义.
In other cases, (e.g. from background threads started in some other way), the current servlet is not automatically defined.
@WebListener
(ServletContextListener
)
顺便说一下,当 Web 应用程序在容器中部署(启动)时,您可能希望处理此类全局状态.
@WebListener
(ServletContextListener
)
By the way, you are likely to want to handle such global state when the web app deploys (launches) in the container.
您可以使用 @WebListener
注释在您的类上实现了 ServletContextListener
接口.该接口的两种方法,contextInitialized
和 contextDestroyed
,被传递一个 ServletContextEvent
,您可以从中访问 ServletContext
对象通过调用 getServletContext
.
You can hook into your Vaadin web app’s deployment with the @WebListener
annotation on your class implementing the ServletContextListener
interface. Both methods of that interface, contextInitialized
and contextDestroyed
, are passed a ServletContextEvent
from which you can access the ServletContext
object by calling getServletContext
.
@WebListener ( "Context listener for doing something or other." )
public class MyContextListener implements ServletContextListener
{
// Vaadin app deploying/launching.
@Override
public void contextInitialized ( ServletContextEvent contextEvent )
{
ServletContext context = contextEvent.getServletContext();
context.setAttribute( … ) ;
// …
}
// Vaadin app un-deploying/shutting down.
@Override
public void contextDestroyed ( ServletContextEvent contextEvent )
{
ServletContext context = contextEvent.getServletContext();
// …
}
}
在执行 Vaadin servlet(或 Web 应用程序中的任何其他 servlet/过滤器)之前,此钩子作为正在初始化的 Vaadin 应用程序的一部分被调用.在 contextInitialized
方法上引用文档:
This hook is called as part of your Vaadin app being initialized, before executing the Vaadin servlet (or any other servlet/filter in your web app). To quote the doc on the contextInitialized
method:
接收到 Web 应用程序初始化过程正在启动的通知.在 Web 应用程序中的任何过滤器或 servlet 初始化之前,所有 ServletContextListener 都会收到上下文初始化通知.
Receives notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.
这篇关于如何从 Vaadin 7 应用程序中访问 `ServletContext`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!