如何使用Struts2在服务器启动时运行操作? [英] How do I run an action on server startup using Struts2?

查看:420
本文介绍了如何使用Struts2在服务器启动时运行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to execute a struts2 action on server startup rather than on the first request.

推荐答案

在启动应用程序时加载数据是一项常见的任务,您会在网上找到一些示例。如其他答案中所述,您应该实现一个ServletContextListener(不是特定于Struts2)...您可以 在此处阅读一个很好的例子

Loading data on startup of an application is a common task, you will find several examples on the web. As said in other answers, you should implement a ServletContextListener (that is not Struts2 specific)... you can read a great example here.

这里重要的是理解Action概念:

The important thing here is understanding the Action concept:

Struts2 MVC (模型视图控制器)框架中,一个 Action 控制器(和 Model 的一部分)。

In Struts2 MVC (Model View Controller) Framework, an Action is the Controller (and part of the Model).

动作由来自客户端请求调用c $ c> s(并且每个请求都会创建一个Action,因此它们是线程安全的。)

Actions are invoked by Requests coming from the Clients (and one Action is created on every request, so they're thread-safe).

这意味着您需要一个Client,即通常指的是坐在电脑前的人,单击浏览器...然后,客户端调用不是在共享对象上执行服务器端自动操作的正确触发器。

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.

当然可以实现某种形式 lazy-initialitazion 的值(例如借助自定义的拦截器),以便第一个用户在Application范围内进行设置,而其他用户将检索已经填充的对象,但这不是最好的方法(您应该处理并发性)在初始化时,您将有一个用户,第一个用户,等待服务器在启动时夜间进行的操作...)。

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...).

这篇关于如何使用Struts2在服务器启动时运行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆