从Struts源码看action调用过程,以下代码片断来自struts1.1
struts中所有的请求都是通过ActionServlet转发的,一个action请求首先会传给 ActionServlet,ActionServlet会将其委托给RequestProcessor,RequestProcessor进行一些处理后将调用对应的Action类来执行该请求。
ActionServlet、RequestProcessor、Action的职责
ActionServlet负责通过Struts配置文件初始化Struts应用程序的配置,并负责接受用户请求。
RequestProcessor负责与请求相关的各方面,如选择与请求相关联的Form Bean、用数据填充Form
Bean、验证Form Bean的有效性及选择正确的Action类来执行该请求。
Action是Struts框架的终点和你的应用程序代码的起点。
以下为Action执行过程的详细分析:
ActionServlet继承自HttpServlet它重写了其doGet、doPost方法,这两个方法执行时都会调用ActionServlet 的process(HttpServletRequest request, HttpServletResponse response)方法,这里将会获取一个RequestProcessor的实例,并调用RequestProcessor的process (HttpServletRequest request, HttpServletResponse response)方法,这样用户请求就转到了RequestProcessor
以下为RequestProcessor类的process(HttpServletRequest request, HttpServletResponse response)方法的片断
// Create or acquire the Action instance to process this request
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
}
// Call the Action instance itself
ActionForward forward =
processActionPerform(request, response,
action, form, mapping);
// Process the returned ActionForward instance
processForwardConfig(request, response, forward);
这里用户请求的Action被实例化后传给processActionPerform(request, response,action, form, mapping)方法,此方法将调用Action的execute(HttpServletRequest request,
HttpServletResponse response,
ActionForm form,
ActionMapping mapping)方法并返回一个ActionForward,
processForwardConfig(request, response, forward)方法将用户界面转到ActionForward指定的url。
至此用户请求的action就执行完毕了。
struts中所有的请求都是通过ActionServlet转发的,一个action请求首先会传给 ActionServlet,ActionServlet会将其委托给RequestProcessor,RequestProcessor进行一些处理后将调用对应的Action类来执行该请求。
ActionServlet、RequestProcessor、Action的职责
ActionServlet负责通过Struts配置文件初始化Struts应用程序的配置,并负责接受用户请求。
RequestProcessor负责与请求相关的各方面,如选择与请求相关联的Form Bean、用数据填充Form
Bean、验证Form Bean的有效性及选择正确的Action类来执行该请求。
Action是Struts框架的终点和你的应用程序代码的起点。
以下为Action执行过程的详细分析:
ActionServlet继承自HttpServlet它重写了其doGet、doPost方法,这两个方法执行时都会调用ActionServlet 的process(HttpServletRequest request, HttpServletResponse response)方法,这里将会获取一个RequestProcessor的实例,并调用RequestProcessor的process (HttpServletRequest request, HttpServletResponse response)方法,这样用户请求就转到了RequestProcessor
以下为RequestProcessor类的process(HttpServletRequest request, HttpServletResponse response)方法的片断
// Create or acquire the Action instance to process this request
Action action = processActionCreate(request, response, mapping);
if (action == null) {
return;
}
// Call the Action instance itself
ActionForward forward =
processActionPerform(request, response,
action, form, mapping);
// Process the returned ActionForward instance
processForwardConfig(request, response, forward);
这里用户请求的Action被实例化后传给processActionPerform(request, response,action, form, mapping)方法,此方法将调用Action的execute(HttpServletRequest request,
HttpServletResponse response,
ActionForm form,
ActionMapping mapping)方法并返回一个ActionForward,
processForwardConfig(request, response, forward)方法将用户界面转到ActionForward指定的url。
至此用户请求的action就执行完毕了。