首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 高级技术 >> 设计模式 >> 正文

设计模式:设计自己的MVC框架

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. <actions>
  3.   <action path="/login"
  4.           class="com.strutslet.demo.LoginAction">
  5.      <forward name="success" url="hello.jsp"/>
  6.      <forward name="fail" url="fail.jsp"/>
  7.    </action>       
  8. </actions>

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.    
  15.   } catch (Exception e) {
  16.    log.error("can not find action "+actionModel.getClassName());
  17.    e.printStackTrace();
  18.   }
  19.   actionForward = actionModel.getForwards().get(forward_name);
  20.   if (actionForward == null) {
  21.    log.error("can not find page for forward "+forward_name);
  22.    return null;
  23.   } else
  24.    return actionForward.getViewUrl();      //返回ActionForward的viewUrl
  25.  }



 

上一页  [1] [2] [3] 下一页

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航