@Yano
2016-06-11T18:52:30.000000Z
字数 2739
阅读 1964
Java-Web
参考链接:http://blog.csdn.net/john2522/article/details/7436307
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:
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的全局数据。必须
同步。当Action第一次被创建时,控制器会调用setServlet方法,并通过一个非空参数标识这个Action所连接的Servlet。当这个Servlet关闭(或重启)时,将会调用setServlet方法,并传递一个null值,可以清理所有被这个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中,你可以自由定义,并不需要实现该接口。
数据如何从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;
}