取这样一个标题太大,吸引眼球嘛@_@。
事实是最近读《j2ee设计模式》讲述表达层模式的那几章,书中有一个前端控制器+command模式的workflow例子,就琢磨着可以很简单地扩展成一个mvc框架。花了一个下午改写了下,对书中所述的理解更为深入。我想这也许对于学习和理解设计模式,以及初次接触struts等mvc框架的人可能有点帮助。因为整个模型类似于struts,我把它取名叫strutslet^_^
(一)完整的类图如下:

1。前端控制器(frontcontroller):前端控制器提供了一个统一的位置来封装公共请求处理,它的任务相当简单,执行公共的任务,然后把请求转交给相应的控制器。在strutslet中,前端控制器主要作用也在于此,它初始化并解析配置文件,接受每个请求,并简单地把请求委托给调度器(dispatcher),由调度器执行相应的动作(action)。调度器把action返回的url返回给frontcontroller, frontcontroller负责转发。
2。action接口:command模式很好的例子,它是一个命令接口,每一个实现了此接口的action都封装了某一个请求:新增一条数据记录并更新model,或者把某个文件写入磁盘。命令解耦了发送者和接受者之间联系。发送者调用一个操作,接受者接受请求执行相应的动作,因为使用command模式解耦,发送者无需知道接受者任何接口。
3。dispatcher:调度器,负责流程的转发,负责调用action去执行业务逻辑。由调度器选择页面和action,它去除了应用行为和前端控制器间的耦合。调度器服务于前端控制器,它把model的更新委托给action,又提供页面选择给frontcontroller
4。actionforward:封装了转向操作所需要信息的一个模型,包括name和转向url
5。actionmodel:解析配置文件后,将每一个action封装成一个actionmodel对象,所有actionmodel构成一个map,并存储在servletcontext中,供整个框架使用。
(二)源代码分析:
1。action接口,只有一个execute方法,任何一个action都只要实现此接口,并实现相应的业务逻辑,最后返回一个actionforward,提供给dispacher调用。
package com.strutslet.core;
import javax.servlet.servletcontext;
import javax.servlet.http.httpservletrequest;
import com.strutslet.model.actionforward;
/**
* command接口
* @author dennis
*
*/
public interface action {
public actionforward execute(httpservletrequest request,servletcontext context);
}
比如,我们要实现一个登陆系统,loginaction验证用户名和密码,如果正确,返回success页面,如果登陆失败,返回fail页面:
package com.strutslet.demo;
import com.strutslet.core.action;
public class loginaction implements action {
private string name = "" ;
public actionforward execute(httpservletrequest request,
servletcontext context) {
string username = request.getparameter( " username " );
string password = request.getparameter( " password " );
if (username.equals( " dennis " ) && password.equals( " 123 " )) {
request.setattribute( " name " , name);
return actionforward.success; // 登陆成功,返回success
} else
return actionforward.fail; // 否则,返回fail
}
2。还是先来看下两个模型:actionforward和actionmodel,没什么东西,属性以及相应的getter,setter方法:
package com.strutslet.model;
* 类说明:转向模型
* */
public class actionforward {
private string name; // forward的name
private string viewurl; // forward的url
public static final actionforward success = new actionforward( " success " );
public static final actionforward fail = new actionforward( " fail " );
public actionforward(string name) {
this .name = name;
}
public actionforward(string name, string viewurl) {
super ();
this .name = name;
this .viewurl = viewurl;
//
name和viewurl的getter和setter方法
}
我们看到actionforward预先封装了success和fail对象。
// actionmodel.java
import java.util.map;
* 类说明:
public class actionmodel {
private string path; // action的path
private string classname; // action的class
private map < string, actionforward > forwards; // action的forward
public actionmodel() {}
public actionmodel(string path, string classname,
map < string, actionforward > forwards) {
this .path = path;
this .classname = classname;
this .forwards = forwards;
相应的getter和setter方法
3。知道了两个模型是什么样,也应该可以猜到我们的配置文件大概是什么样的了,与struts的配置文件格式类似:
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< actions >
< action path = " /login "
class = " com.strutslet.demo.loginaction " >
< forward name = " success " url = " hello.jsp " />
< forward name = " fail " url = " fail.jsp " />
</ action >
</ actions >
path是在应用中将被调用的路径,class指定了调用的哪个action,forward元素指定了转向,比如我们这里如果是success就转向hello.jsp,失败的话转向fail.jsp,这里配置了demo用到的loginaction。
4。dispacher接口,主要是getnextpage方法,此方法负责获得下一个页面将导向哪里,提供给前端控制器转发。
* service to worker模式,提供给frontcontroller使用
* 负责流程转发
public interface dispatcher {
public void setservletcontext(servletcontext context);
public string getnextpage(httpservletrequest request,servletcontext context);
5。原先书中实现了一个workflow的dispatcher,按照顺序调用action,实现工作流调用。而我们所需要的是根据请求的path 调用相应的action,执行action的execute方法返回一个actionforward,然后得到actionforward的 viewurl,将此viewurl提供给前端控制器转发,看看它的getnextpage方法:
public string getnextpage(httpservletrequest request, servletcontext context) {
setservletcontext(context);
map < string, actionmodel > actions = (map < string, actionmodel > ) context
.getattribute(constant.actions_attr); // 从servletcontext得到所有action信息
string reqpath = (string) request.getattribute(constant.request_attr); // 发起请求的path
actionmodel actionmodel = actions.get(reqpath); // 根据path得到相应的action
string forward_name = "" ;
actionforward actionforward;
try {
class c = class.forname(actionmodel.getclassname()); // 每个请求对应一个action实例
action action = (action) c.newinstance();
actionforward = action.execute(request, context); // 执行action的execute方法
forward_name = actionforward.getname();
} catch (exception e) {
log.error( " can not find action " + actionmodel.getclassname());
e.printstacktrace();
actionforward = actionmodel.getforwards().get(forward_name);
if (actionforward == null ) {
log.error( " can not find page for forward " + forward_name);
return null ;
} else
return actionforward.getviewurl(); // 返回actionforward的viewurl
6。前端控制器(frontcontroller),它的任务我们已经很清楚,初始化配置文件;存储所有action到 servletcontext供整个框架使用;得到发起请求的path,提供给dispachter查找相应的action;调用dispatcher,执行getnextpage方法得到下一个页面的url并转发:
public void init() throws servletexception {
// 初始化配置文件
servletcontext context = getservletcontext();
string config_file = getservletconfig().getinitparameter( " config " );
string dispatcher_name = getservletconfig().getinitparameter( " dispatcher " );
if (config_file == null || config_file.equals( "" ))
config_file = " /web-inf/strutslet-config.xml " ; // 默认是/web-inf/下面的strutslet-config
if (dispatcher_name == null || dispatcher_name.equals( "" ))
dispatcher_name = constant.default_dispatcher;
map < string, actionmodel > resources = configutil.newinstance() // 工具类解析配置文件
.parse(config_file, context);
context.setattribute(constant.actions_attr, resources); // 存储在servletcontext中
log.info( " 初始化strutslet配置文件成功 " );
log.error( " 初始化strutslet配置文件失败 " );
// 实例化dispacher
try {
class c = class.forname(dispatcher_name);
dispatcher dispatcher = (dispatcher) c.newinstance();
context.setattribute(constant.dispatcher_attr, dispatcher); // 放在servletcontext
log.info( " 初始化dispatcher成功 " );
} catch (exception e) {
log.error( " 初始化dispatcher失败 " );
e.printstacktrace();
..
doget()和dopost方法我们都让它调用process方法:
protected void process(httpservletrequest request,
httpservletresponse response) throws servletexception, ioexception {
servletcontext context = getservletcontext();
// 获取action的path
string requri = request.getrequesturi();
int i = requri.lastindexof( " . " );
string contextpath = request.getcontextpath();
string path = requri.substring(contextpath.length(),i);
request.setattribute(constant.request_attr, path);
dispatcher dispatcher = (dispatcher) context.getattribute(constant.dispatcher_attr);
// make sure we don't cache dynamic data
response.setheader( " cache-control " , " no-cache " );
response.setheader( " pragma " , " no-cache " );
// use the dispatcher to find the next page
string nextpage = dispatcher.getnextpage(request, context); // 调用dispatcher的getnextpage
// forward control to the view
requestdispatcher forwarder = request.getrequestdispatcher( " / "
+ nextpage);
forwarder.forward(request, response); // 转发页面
7。最后,web.xml的配置就非常简单了,配置前端控制器,提供启动参数(配置文件所在位置,为空就查找/web-inf/下面的strutslet-config.xml文件),我们把所有以action结尾的请求都交给frontcontroller处理:
< servlet >
< servlet - name > strutsletcontroller </ servlet - name >
< servlet - class > com.strutslet.core.frontcontroller </ servlet - class >
<!--
< init - param >
< param - name > config </ param - name >
< param - value >/ web - inf / strutslet - config.xml </ param - value >
</ init - param >
-->
< load - on - startup > 0 </ load - on - startup >
</ servlet >
< servlet - mapping >
< url - pattern >* .action </ url - pattern >
</ servlet - mapping >
文章转自庄周梦蝶 ,原文发布5.16