loading ...
loading...

2006-12-02 | 设计自己的MVC框架

标签: 控制器  模型  调度  方法  调用 

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

    事实是最近读《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 javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.strutslet.core.Action;
import com.strutslet.model.ActionForward;

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;

/**
 * 类说明:转向模型
 * @author dennis
 *
 * */
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

package com.strutslet.model;

import java.util.Map;

/**
 * 类说明:
 * @author dennis
 *
 */

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) {
  super();
  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方法,此方法负责获得下一个页面将导向哪里,提供给前端控制器转发。

package com.strutslet.core;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

/**
 * service to worker模式,提供给FrontController使用
 * 负责流程转发
 * @author dennis
 *
 */
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;
    
  try {
   Map<String, ActionModel> resources = ConfigUtil.newInstance()  //工具类解析配置文件
     .parse(config_file, context);
   context.setAttribute(Constant.ACTIONS_ATTR, resources);  //存储在ServletContext中
   log.info("初始化strutslet配置文件成功");
  } catch (Exception e) {
   log.error("初始化strutslet配置文件失败");
   e.printStackTrace();
  }

  //实例化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>
    <servlet-name>StrutsletController</servlet-name>
    <url-pattern>*.action</url-pattern>
 </servlet-mapping>

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

评论 (7) |  阅读 (?)  |  固定链接 |  类别 (设计模式) |  发表于 15:22  | 最后修改于 2006-12-07 19:35
正在读取评论信息...
您还未登录,只能匿名发表评论。或者您可以 登录 后发表。
 
  *中国人爱国心,搜狗输入法爱国主题皮肤下载>>
表  情:
加载中...
回复通知: 同时用小纸条通知对方该回复