简体   繁体   English

如何使用Struts2在服务器启动时运行操作?

[英]How do I run an action on server startup using Struts2?

我必须在服务器启动时而不是在第一个请求上执行struts2操作。

Loading data on startup of an application is a common task, you will find several examples on the web. 在启动应用程序时加载数据是一项常见任务,您可以在Web上找到几个示例。 As said in other answers, you should implement a ServletContextListener (that is not Struts2 specific)... you can read a great example here . 正如在其他答案中所说,你应该实现一个ServletContextListener(这不是Struts2特定的)...你可以在这里阅读一个很好的例子

The important thing here is understanding the Action concept: 这里重要的是理解行动概念:

In Struts2 MVC (Model View Controller) Framework, an Action is the Controller (and part of the Model ). Struts2 MVC (模型视图控制器)框架中, ActionController (和Model一部分)。

Action s are invoked by Request s coming from the Client s (and one Action is created on every request, so they're thread-safe). 来自ClientRequest来调用Action (并且每个请求都创建一个Action,因此它们是线程安全的)。

This means that you need a Client, that usually means a guy in front of a pc, clicking on a browser... then, a client call is not the right trigger to perform automated, server-side operation on shared objects. 这意味着您需要一个客户端,这通常意味着在PC前面的人,点击浏览器...然后,客户端调用不是在共享对象上执行自动服务器端操作的正确触发器。

Of course you could implement some form of lazy-initialitazion (eg. with the help of a custom Interceptor) so that the first user would set-up something in the Application scope, and the other users would retrieve the object already populated, but this is not the best way to do it (you should handle the concurrency on the initialitaion, and you would have one user, the first, waiting for operations that the server could have done in the night on startup...). 当然你可以实现某种形式的lazy-initialitazion (例如,在自定义拦截器的帮助下),这样第一个用户就可以在Application范围内设置一些东西,而其他用户将检索已经填充的对象,但是这个这不是最好的方法(您应该在初始化时处理并发性,并且您将有一个用户,第一个,等待服务器在启动时可以在晚上完成的操作...)。

Write a ServletContextListener, this will be available only one per web application and will get instatiated when the application is deployed. 编写一个ServletContextListener,每个Web应用程序只能使用一个,并在部署应用程序时进行实例化。

Here is the post 这是帖子

Load on start-up in servlet and jsp is present as below servlet和jsp启动时的加载如下所示

You can ask the page to be loaded when the server starts. 您可以在服务器启动时要求加载页面。 This is done via the web.xml file 这是通过web.xml文件完成的

<servlet>
<servlet-name>login</servlet-name>
<jsp-file>/login.jsp</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>

Normally jsp file is compiles on first hit. 通常jsp文件在第一次命中时编译。 Now the code says precompile a jsp file without waiting for the first hit. 现在代码说预编译一个jsp文件而不等待第一次命中。

For struts2 you can change programatically in web.xml 

<listener>
    <listener-class>your listener class</listener-class>
</listener>

refer this link it might be helpful to you 请参考此链接,它可能对您有所帮助

Loadonstart up . Loadonstart up

If you want to some code to run when your web application , aka Servlet Context , starts for the first time, then you should leverage the hooks provided by the technology. 如果您希望在Web应用程序 (即Servlet Context)首次启动时运行某些代码,那么您应该利用该技术提供的钩子。 The Servlet API provides lifecycle hooks for you to use to fire code at various lifecycle stages of a web application. Servlet API提供生命周期钩子,供您在Web应用程序的各个生命周期阶段触发代码。 Since all Struts 2 applications are Servlet API web applications, then you can leverage this yourself. 由于所有Struts 2应用程序都是Servlet API Web应用程序,因此您可以自己利用它。

The ServletContextListener interface provides an init hook method. ServletContextListener接口提供了一个init钩子方法。 You simply implement this interface and register your implementation in the web.xml. 您只需实现此接口并在web.xml中注册您的实现。

Note, if what you need to do is more Struts 2 specific, then you could consider utilizing something from within the Struts 2 API itself. 注意,如果你需要做的是更具体的Struts 2,那么你可以考虑使用Struts 2 API本身的东西。

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

相关问题 Struts2:我如何告诉我的index.jsp转发到struts2动作? - Struts2: How do I tell my index.jsp to forward to a struts2 action? Java Struts2-如何使用struts标签创建要由用户填充的arraylist,然后将值传递给操作? - Java Struts2 - How do I create an arraylist using struts tags, to be populated by user then the values should be passed to action? Struts2-如何在不使用struts.xml的情况下重定向到动作? - Struts2 - How to redirect to an action without using struts.xml? Struts2:如何监控SERVER SIDE Action流的进度? - Struts2: How can I monitor the progress of an SERVER SIDE Action streaming? 如何在Struts2中采取行动 - how to action fire in Struts2 我如何使用Struts2将隐藏值发送到动作类 - How can i send a hidden value to action class using Struts2 如何在不使用任何操作类的情况下使用Struts2约定 - How to use Struts2 convention without using any action class 如何使用jquery文件上传插件在struts2中激活动作 - how activate action in struts2 using jquery file upload plugin 如何使用注释将struts2 action的实例化委托给spring - how to delegate struts2 action's instantiation to spring using annotations Struts2:如何在迭代器中使用HashMap的键进行比较 - Struts2: How do I do a comparison using a HashMap's key inside an iterator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM