Struts 2 调度程序
我只想(在 Struts2 中)初始化一个属性(从文件加载数据)一次,并使该属性可用于整个 struts 2 应用程序。 我怎样才能做到这一点?我需要覆盖 struts 2 调度程序吗?
问候 拉朱
i would like to initialize (in Struts2) a property(data loading from a file) only once and make available that property for entire struts 2 application.
how can i achieve that? do i need override struts 2 dispatcher?
Regards
Raju
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个在
web.xml
中定义的ServletContextListener
,它打开属性文件并通过以下方式将所需的值设置为ServletContext
: ServletContext 具有应用程序范围的范围。
更新:
您可以创建一个实现
ServletContextListener
的新类(这是其 JavaDoc:ServletContextListener),这要求您定义contextInitialized()
和contextDestroyed()方法。
contextInitialized()
方法在您的 servlet 开始接受请求之前被调用。在contextInitialized()
方法中,您将包含getServletContext().setAttribute("dataKey", dataValue)
调用。为了注册您的侦听器,您需要在
web.xml
文件中添加侦听器定义:您需要将上述 XML 中的 CLASS_PATH.CLASS_NAME 替换为侦听器的类路径和名称您刚刚创建的上下文侦听器类。
You could create a
ServletContextListener
defined inweb.xml
that opens your property file and sets the desired value to theServletContext
via:The
ServletContext
has application-wide scope.Update:
You can create a new class that implements
ServletContextListener
(here is its JavaDoc: ServletContextListener), which requires that you definecontextInitialized()
andcontextDestroyed()
methods.The method
contextInitialized()
is called right before your servlet begins accepting requests. In yourcontextInitialized()
method, you would include thegetServletContext().setAttribute("dataKey", dataValue)
call.In order to register your listener, you will need to add a listener definition in your
web.xml
file:You'll need to replace CLASS_PATH.CLASS_NAME in the above XML with the class path and name of the context listener class you just created.