在Struts中Action控制类是整个框架的核心,它连接着页面请求和后台业务逻辑处理。按照框架设计,每一个继承自Action的子类,都实现execute方法——调用后台真正处理业务的对象来完成任务。
注:继承自DispatchAction的子类,则可以一个类里面处理多个类似的操作。这个在这不做讨论。
下面我们将Struts中的各个类与命令模式中的角色对号入座。
先来看下命令角色——Action控制类
public class Action {
……
/*
*可以看出,Action中提供了两个版本的执行接口,而且实现了默认的空实现。
*/
public ActionForward execute( ActionMapping mapping,
ActionForm form,
ServletRequest request,
ServletResponse response)
throws Exception {
try {
return execute(mapping, form, (HttpServletRequest) request,
(HttpServletResponse) response);
} catch (ClassCastException e) {
return null;
}
}
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return null;
}
}
下面的就是请求者角色,它仅仅负责调用命令角色执行操作。
public class RequestProcessor {
……
protected ActionForward processActionPerform(HttpServletRequest request,
HttpServletResponse response,
Action action,
ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
在Struts中Action控制类是整个框架的核心,它连接着页面请求和后台业务逻辑处理。按照框架设计,每一个继承自Action的子类,都实现execute方法——调用后台真正处理业务的对象来完成任务。
注:继承自DispatchAction的子类,则可以一个类里面处理多个类似的操作。这个在这不做讨论。
下面我们将Struts中的各个类与命令模式中的角色对号入座。
先来看下命令角色——Action控制类
public class Action {
……
/*
*可以看出,Action中提供了两个版本的执行接口,而且实现了默认的空实现。
*/
public ActionForward execute( ActionMapping mapping,
ActionForm form,
ServletRequest request,
ServletResponse response)
throws Exception {
try {
return execute(mapping, form, (HttpServletRequest) request,
(HttpServletResponse) response);
} catch (ClassCastException e) {
return null;
}
}
public ActionForward execute( ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
return null;
}
}
下面的就是请求者角色,它仅仅负责调用命令角色执行操作。
public class RequestProcessor {
……
protected ActionForward processActionPerform(HttpServletRequest request,
HttpServletResponse response,
Action action,
ActionForm form,
ActionMapping mapping)
throws IOException, ServletException {
try {
return (action.execute(mapping, form, request, response));
} catch (Exception e) {
return (processException(request, response,e, form, mapping));
}
}
}
Struts框架为我们提供了以上两个角色,要使用struts框架完成自己的业务逻辑,剩下的三个角色就要由我们自己来实现了。步骤如下:
1) 很明显我们要先实现一个Action的子类,并重写execute方法。在此方法中调用业务模块的相应对象来完成任务。
2) 实现处理业务的业务类。
3) 配置struts-config.xml配置文件,将自己的Action和Form以及相应页面结合起来。
4) 编写jsp,在页面中显式的制定对应的处理Action。
一个完整的命令模式就介绍完了。当你在页面上提交请求后,Struts框架会根据配置文件中的定义,将你的Action对象作为参数传递给RequestProcessor类中的processActionPerform()方法,由此方法调用Action对象中的执行方法,进而调用业务层中的接收角色。这样就完成了请求的处理。
return (processException(request, response,e, form, mapping));
}
}
}
Struts框架为我们提供了以上两个角色,要使用struts框架完成自己的业务逻辑,剩下的三个角色就要由我们自己来实现了。步骤如下:
1) 很明显我们要先实现一个Action的子类,并重写execute方法。在此方法中调用业务模块的相应对象来完成任务。
2) 实现处理业务的业务类。
3) 配置struts-config.xml配置文件,将自己的Action和Form以及相应页面结合起来。
4) 编写jsp,在页面中显式的制定对应的处理Action。
一个完整的命令模式就介绍完了。当你在页面上提交请求后,Struts框架会根据配置文件中的定义,将你的Action对象作为参数传递给RequestProcessor类中的processActionPerform()方法,由此方法调用Action对象中的执行方法,进而调用业务层中的接收角色。这样就完成了请求的处理。
命令模式还有一个常见的用法就是执行事务操作。这就是为什么命令模式还叫做事务模式的原因吧。它可以在请求被传递到接收者角色之前,检验请求的正确性,甚至可以检查和数据库中数据的一致性,而且可以结合组合模式的结构,来一次执行多个命令。
使用命令模式不仅仅可以解除请求者和接收者之间的耦合,而且可以用来做批处理操作,这完全可以发挥你自己的想象——请求者发出的请求到达命令角色这里以后,先保存在一个列表中而不执行;等到一定的业务需要时,命令模式再将列表中全部的操作逐一执行。