您的位置:首页 > 编程语言 > Java开发

Struts1 和 Struts2 对比

2016-06-18 10:52 513 查看
参考链接: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对比



Action模型

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

Struts1

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

Struts2

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

参数
:Struts1的execute方法,是具有参数的;Struts2没有。

返回类型
:Struts1的返回类型是ActionForward;Struts2是String。

调用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts 源代码 struts2.0