Struts2 操作与 Servlet 相比如何?
Struts2 操作与 Servlet 相比如何? Action 可以充当 servlet 吗?
How do Struts2 actions compare to Servlets? Can an action act as a servlet?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Struts(Struts1/Struts classic)动作与 servlet 的联系更为紧密。在Struts2 中,情况就完全不同了。 Struts2 操作只是一个 POJO(普通 Java 类),与 Servlet API 完全解耦。这种解耦简化了测试。
在 Struts2 Web 应用程序的典型工作流程中,将为每个请求实例化一个操作,并将其与 Servlet 关联(如果需要了解这种关联,它可以实现 ServletAware 接口;通常这没有必要也不建议)。
与 Servlet(以及 Struts 操作)的一个重要概念差异是 Struts2 操作不会针对不同的请求重用,因此是线程安全的:比如说,可能会出现三个 http 请求(同时或不同时)由一个 servlet 实例提供服务的情况;但在这种情况下,我们仍然会拥有三个不同的 Struts2 操作实例,每个请求一个实例。
A Struts (Struts1/Struts classic) action was more tied to a servlet. In Struts2, things are quite different. A Struts2 action is just a POJO (plain Java class), totally decoupled from the Servlet API. This decoupling eases testing.
In the typical workflow of a Struts2 webapp, an action will be instantiated for each request and will be associated with a Servlet (it can implement the
ServletAware
interface if it needs to be aware of this association; normally this is not necessary nor advisable).An important conceptual difference with Servlets (and with Struts actions) is that Struts2 actions are not reused for different requests, and hence are thread safe: say, it can happen that three http requests (simultaneous or not) are served by one servlet instance; but inthat case we will still have three different Struts2 action instances, one for each request.
Struts 是普通 java servlet 之上的一个抽象层。操作本身由程序员定义,并在点击 URL 时由 struts 框架调用(您配置哪个 url 映射到哪个操作)。因此,它们并不真正与 servlet“比较”,它们是围绕 servlet 提供的功能的抽象。使用操作所做的一件典型的事情是输出一个 jsp,它相当于一个 servlet。所以发生的事情是
a) 请求进来,被映射到操作
b) 操作加载一些数据
c) action 呈现一个 jsp,将加载的数据传递给 jsp。
如果您想要的话,操作可以直接输出到请求/响应,但在大多数情况下可能不是一个好的做法。
Struts is an abstraction layer on top of the vanilla java servlet stuff. Actions themselves are defined by the programmer and are invoked by struts frameworks when a URL is hit (you configure what url maps to which action). So they don't really "compare" to a servlet, they are an abstraction around the functionality the servlet provides. One typical thing you do with an action is output a jsp, which is equivalent to a servlet. so what happens is
a) request comes in, gets mapped to action
b) action loads some data
c) action renders a jsp, passing loaded data to the jsp.
An action can output directly to the request/response, if that is what you want, but in most cases is probably not good practice.
Struts2 是一个基于 MVC 框架实现Java EE 技术。
Struts2 is a MVC framework implementation based on Java EE technology.