首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Struts1 和 Struts2 对比

Struts1 和 Struts2 对比

作者头像
Yano_nankai
发布2018-10-08 10:51:53
4350
发布2018-10-08 10:51:53
举报
文章被收录于专栏:二进制文集二进制文集

参考链接:http://blog.csdn.net/john2522/article/details/7436307

Struts1 Action 官方注释

Action.java 源代码中的注释如下:

An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (RequestProcessor) will select an appropriate Action for each request, create an instance (if necessary), and call the execute method.

Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:

  • Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
  • Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.

When an Action instance is first created, the controller will call setServlet with a non-null argument to identify the servlet instance to which this Action is attached. When the servlet is to be shut down (or restarted), the setServlet method will be called with a null argument, which can be used to clean up any allocated resources in use by this Action.


中文翻译如下:

Action是服务器接收到的HTTP request与需要执行的业务逻辑间的枢纽。控制器(RequestProcessor)会给每个request选择合适的Action,创建新的实例(必要的话),并执行其中的execute方法。

Actions必须是线程安全的,因为控制器也会在多个并发请求下共享同一个实例。这意味着你需要牢记以下几点:

  • 实例和静态变量不能用于存储关于特定请求的信息。它们可以用来共享同一Action的多个requests的全局数据。
  • 访问其它资源(JavaBean、session等)必须同步。

当Action第一次被创建时,控制器会调用setServlet方法,并通过一个非空参数标识这个Action所连接的Servlet。当这个Servlet关闭(或重启)时,将会调用setServlet方法,并传递一个null值,可以清理所有被这个Action所占用的资源。


Struts2 Action 官方注释

源代码的英文注释如下:

All actions may implement this interface, which exposes the execute() method.

However, as of XWork 1.1, this is not required and is only here to assist users. You are free to create POJOs that honor the same contract defined by this interface without actually implementing the interface.


中文翻译如下:

所有的Action可能实现这个接口,暴露了execute()方法。

对于XWork 1.1,这个接口并不是必须的,只是用来辅助使用者。在POJO中,你可以自由定义,并不需要实现该接口。


Struts1和2的Action对比

Struts1和2的区别.png-154.7kB
Struts1和2的区别.png-154.7kB

Struts1和2的区别.png-154.7kB

Action模型

数据如何从Action中,传入JSP中?

Struts1

需要显示的数据(Bean),要在Action中存到Request或Session中。Struts1必须继承org.apache.struts.action.Action或者其子类,表单数据封装在FormBean中。

Struts2

表单数据包含在Action中,通过Getter和Setter获取,无需继承任何类型或实现任何接口。


  1. 参数:Struts1的execute方法,是具有参数的;Struts2没有。
  2. 返回类型:Struts1的返回类型是ActionForward;Struts2是String。
  3. 调用Action:Struts1只能通过execute方法调用;Struts2任何声明为public String methodName() 方法,都能通过配置来调用Action。

Struts1的execute方法:

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        return super.execute(mapping, form, request, response);
    }

Struts2的execute方法:

    @Override
    public String execute() {
        return LOGIN;
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.06.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Struts1 Action 官方注释
  • Struts2 Action 官方注释
  • Struts1和2的Action对比
  • Action模型
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档