Struts1 和 Struts2 对比
2016-06-18 10:52
513 查看
参考链接:http://blog.csdn.net/john2522/article/details/7436307
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
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
中文翻译如下:
Actions必须是线程安全的,因为控制器也会在多个并发请求下共享
实例和静态变量
访问其它资源(JavaBean、session等)
当Action第一次被创建时,控制器会调用setServlet方法,并通过一个非空参数标识这个Action所连接的Servlet。当这个Servlet关闭(或重启)时,将会调用setServlet方法,并传递一个null值,可以清理所有被这个Action所占用的资源。
All actions may implement this interface, which exposes the
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
对于XWork 1.1,这个接口并不是必须的,只是用来辅助使用者。在POJO中,你可以自由定义,并不需要实现该接口。
Struts1
需要显示的数据(Bean),要在Action中存到Request或Session中。Struts1必须继承org.apache.struts.action.Action或者其子类,表单数据封装在FormBean中。
Struts2
表单数据包含在Action中,通过Getter和Setter获取,无需继承任何类型或实现任何接口。
Struts1的execute方法:
Struts2的execute方法:
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
executemethod.
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
Actioninstance is first created, the controller will call
setServletwith 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
setServletmethod will be called with a
nullargument, 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; }
相关文章推荐
- JQuery+Strusts1.x无刷新登录
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- C#获取网页源代码的方法
- LCL.VBS 病毒源代码
- ASP常用源代码的总结(上)
- php查看网页源代码的方法
- java struts常见错误以及原因分析
- Struts之logic标签库详解
- 通过实例深入学习Java的Struts框架中的OGNL表达式使用
- Java的Struts框架中append标签与generator标签的使用
- Lua中的源代码预编译浅析
- ASP常用源代码的总结(下)
- android private libraries 中的包源代码添加方法
- struts2的select标签用法实例分析
- SSH框架网上商城项目第20战之在线支付平台
- 在Java的Struts框架下进行web编程的入门教程
- 详解Java的Struts框架中注释的用法
- java中struts 框架的实现
- 利用Java的Struts框架实现电子邮件发送功能
- 在Java的Struts框架中ONGL表达式的基础使用入门