为什么struts2的action类中不需要main()?
*大家好..! *
通常java程序需要main()函数来执行代码,否则会返回错误[java.lang.NoSuchMethodError: main Exception in thread "main"]..!在struts2中,我们使用动作类和动作类。 bean 类(java 代码),但它们不需要 main() 函数。这背后的概念是什么?顺便说一句,我是 Struts 框架的新手。
*Hello Guys..! *
Normally a java program needs main() function to execute the code otherwise it'll returns an error[java.lang.NoSuchMethodError: main Exception in thread "main"]..! In struts2 we are using action classes & bean classes(java codes) but they don't needs main() function in it. What is the concept behind this..? By the way I'm a newbie to the Struts framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 Struts 框架提供了启动和运行应用程序的基础设施。作为框架的用户,您可以实现由框架代码调用的特定类。
Struts 本身运行在应用程序服务器(例如 Tomcat)内,通常该服务器包含最终负责启动应用程序的
main()
方法。这是控制反转的示例——许多应用程序框架都是这样工作的。
This is because the Struts framework provides the infrastructure to get the application up and running. As a user of the framework, you implement specific classes that are called by the framework code.
Struts itself runs inside an application server such as Tomcat, and usually it is the server that contains the
main()
method which is ultimately responsible for starting up your application.This is an example of inversion of control -- many application frameworks work this way.
在从命令行启动的应用程序中,您需要一个 main() 方法,因为这是 JVM 期望在这种情况下执行的 API。从其他框架内调用的代码必须实现该框架所需的任何 API。在许多情况下,框架实现 main() 并通过其他一些 API 调用您的代码。这通常比 public void main(String[] args) 更强大。在其他情况下,框架本身是从另一个抽象调用的(例如,应用程序容器内的Web框架内的Web应用程序)。
You need a main() method in applications started from the command line as that's the API that the JVM expects to execute in that case. Code called from within other frameworks must implement whatever API that framework expects. In many cases that framework implements main() and calls your code via some other API. This is usually more robust than
public void main(String[] args)
allows. In other instances the framework is itself called from another abstraction (e.g. a Web Application within a Web Framework within a App Container).