[转载]设计自己的MVC框架

设计自己的MVC框架
首发在 我的blog

源代码放在sharesources@126.com的邮箱的收件箱里,用户名:sharesource,密码:javafans
希望保留给有用的人,谢谢。

取这样一个标题太大,吸引眼球嘛@_@。

事实是最近读《J2EE设计模式》讲述表达层模式的那几章,书中有一个前端控制器+command模式的workflow例子,就琢磨着可以很简单地扩展 成一个MVC框架。花了一个下午改写了下,对书中所述的理解更为深入。我想这也许对于学习和理解设计模式,以及初次接触struts等MVC框架的人可能 有点帮助。因为整个模型类似于struts,我把它取名叫strutslet^_^。学习性质,切勿认真。

(一)完整的类图如下:
strutslet.png

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调用。
  1. public interface Action {
  2. public ActionForward execute(HttpServletRequest request,ServletContext context);
  3. }


比如,我们要实现一个登陆系统(demo的例子),LoginAction验证用户名和密码,如果正确,返回success页面,如果登陆失败,返回fail页面:
  1. public class LoginAction implements Action {
  2. private String name="";
  3. public ActionForward execute(HttpServletRequest request,
  4. ServletContext context) {
  5. String userName=request.getParameter("userName");
  6. String password=request.getParameter("password");
  7. if(userName.equals("dennis")&&password.equals("123")){
  8. request.setAttribute("name", name);
  9. return ActionForward.SUCCESS; //登陆成功,返回success
  10. }else
  11. return ActionForward.FAIL; //否则,返回fail
  12. }


2.还是先来看下两个模型:ActionForward和ActionModel,没什么东西,属性以及相应的getter,setter方法:
  1. /**
  2. * 类说明:转向模型
  3. * @author dennis
  4. *
  5. * */
  6. public class ActionForward {
  7. private String name; //forward的name
  8. private String viewUrl; //forward的url
  9. public static final ActionForward SUCCESS=new ActionForward("success");
  10. public static final ActionForward FAIL=new ActionForward("fail");
  11. public ActionForward(String name){
  12. this.name=name;
  13. }
  14. public ActionForward(String name, String viewUrl) {
  15. super();
  16. this.name = name;
  17. this.viewUrl = viewUrl;
  18. }
  19. //...name和viewUrl的getter和setter方法
  20. }

我们看到ActionForward预先封装了SUCCESS和FAIL对象。
  1. public class ActionModel {
  2. private String path; // action的path
  3. private String className; // action的class
  4. private Map<String, ActionForward> forwards; // action的forward
  5. public ActionModel(){}
  6. public ActionModel(String path, String className,
  7. Map<String, ActionForward> forwards) {
  8. super();
  9. this.path = path;
  10. this.className = className;
  11. this.forwards = forwards;
  12. }
  13. //...相应的getter和setter方法
  14. }


3。知道了两个模型是什么样,也应该可以猜到我们的配置文件大概是什么样的了,与struts的配置文件格式类似:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. "/login"
  3. class="com.strutslet.demo.LoginAction">
  4. "success" url="hello.jsp"/>
  5. "fail" url="fail.jsp"/>

path是在应用中将被调用的路径,class指定了调用的哪个action,forward元素指定了转向,比如我们这里如果是success就转向hello.jsp,失败的话转向fail.jsp,这里配置了demo用到的LoginAction。

4。Dispacher接口,主要是getNextPage方法,此方法负责获得下一个页面将导向哪里,提供给前端控制器转发。
  1. public interface Dispatcher {
  2. public void setServletContext(ServletContext context);
  3. public String getNextPage(HttpServletRequest request,ServletContext context);
  4. }


5。 5。原先书中实现了一个WorkFlow的Dispatcher,按照顺序调用action,实现工作流调用。而我们所需要的是根据请求的path调用相 应的action,执行action的execute方法返回一个ActionForward,然后得到ActionForward的viewUrl,将 此viewUrl提供给前端控制器转发,看看它的getNextPage方法:

  1. public String getNextPage(HttpServletRequest request, ServletContext context) {
  2. setServletContext(context);
  3. Map<String, ActionModel> actions = (Map<String, ActionModel>) context
  4. .getAttribute(Constant.ACTIONS_ATTR); //从ServletContext得到所有action信息
  5. String reqPath = (String) request.getAttribute(Constant.REQUEST_ATTR);//发起请求的path
  6. ActionModel actionModel = actions.get(reqPath); //根据path得到相应的action
  7. String forward_name = "";
  8. ActionForward actionForward;
  9. try {
  10. Class c = Class.forName(actionModel.getClassName()); //每个请求对应一个action实例
  11. Action action = (Action) c.newInstance();
  12. actionForward = action.execute(request, context); //执行action的execute方法
  13. forward_name = actionForward.getName();
  14. } catch (Exception e) {
  15. log.error("can not find action "+actionModel.getClassName());
  16. e.printStackTrace();
  17. }
  18. actionForward = actionModel.getForwards().get(forward_name);
  19. if (actionForward == null) {
  20. log.error("can not find page for forward "+forward_name);
  21. return null;
  22. } else
  23. return actionForward.getViewUrl(); //返回ActionForward的viewUrl
  24. }


6。 前端控制器(FrontController),它的任务我们已经很清楚,初始化配置文件;存储所有action到ServletContext供整个框 架使用;得到发起请求的path,提供给Dispachter查找相应的action;调用Dispatcher,执行getNextPage方法得到下 一个页面的url并转发:

  1. public void init() throws ServletException {
  2. //初始化配置文件
  3. ServletContext context=getServletContext();
  4. String config_file =getServletConfig().getInitParameter("config");
  5. String dispatcher_name=getServletConfig().getInitParameter("dispatcher");
  6. if (config_file == null || config_file.equals(""))
  7. config_file = "/WEB-INF/strutslet-config.xml"; //默认是/WEB-INF/下面的strutslet-config
  8. if(dispatcher_name==null||dispatcher_name.equals(""))
  9. dispatcher_name=Constant.DEFAULT_DISPATCHER;
  10. try {
  11. Map<String, ActionModel> resources = ConfigUtil.newInstance() //工具类解析配置文件
  12. .parse(config_file, context);
  13. context.setAttribute(Constant.ACTIONS_ATTR, resources); //存储在ServletContext中
  14. log.info("初始化strutslet配置文件成功");
  15. } catch (Exception e) {
  16. log.error("初始化strutslet配置文件失败");
  17. e.printStackTrace();
  18. }
  19. //实例化Dispacher
  20. try{
  21. Class c = Class.forName(dispatcher_name);
  22. Dispatcher dispatcher = (Dispatcher) c.newInstance();
  23. context.setAttribute(Constant.DISPATCHER_ATTR, dispatcher); //放在ServletContext
  24. log.info("初始化Dispatcher成功");
  25. }catch(Exception e) {
  26. log.error("初始化Dispatcher失败");
  27. e.printStackTrace();
  28. }
  29. .....


doGet()和doPost方法我们都让它调用process方法:
  1. protected void process(HttpServletRequest request,
  2. HttpServletResponse response) throws ServletException, IOException {
  3. ServletContext context = getServletContext();
  4. //获取action的path
  5. String reqURI = request.getRequestURI();
  6. int i=reqURI.lastIndexOf(".");
  7. String contextPath=request.getContextPath();
  8. String path=reqURI.substring(contextPath.length(),i);
  9. request.setAttribute(Constant.REQUEST_ATTR, path);
  10. Dispatcher dispatcher = (Dispatcher) context.getAttribute(Constant.DISPATCHER_ATTR);
  11. // make sure we don't cache dynamic data
  12. response.setHeader("Cache-Control", "no-cache");
  13. response.setHeader("Pragma", "no-cache");
  14. // use the dispatcher to find the next page
  15. String nextPage = dispatcher.getNextPage(request, context);//调用Dispatcher的getNextPage
  16. // forward control to the view
  17. RequestDispatcher forwarder = request.getRequestDispatcher("/"
  18. + nextPage);
  19. forwarder.forward(request, response); //转发页面
  20. }


7。最后,web.xml的配置就非常简单了,配置前端控制器,提供启动参数(配置文件所在位置,为空就查找/WEB-INF/下面的strutslet-config.xml文件),我们把所有以action结尾的请求都交给FrontController处理:
  1. StrutsletController
  2. class>com.strutslet.core.FrontControllerclass>
  3. <!--
  4. config
  5. /WEB-INFstrutslet-config.xml
  6. --&gt
  7. 0
  8. StrutsletController
  9. *.action


最后,让我们看看整个框架图:
temp.png

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-132259/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/374079/viewspace-132259/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值