国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

設計模式:設計自己的MVC框架

2019-11-17 04:37:01
字體:
來源:轉載
供稿:網友
源代碼放在sharesources@126.com的郵箱的收件箱里,用戶名:sharesource,密碼:javafans
希望保留給有用的人,謝謝。

    取這樣一個標題太大,吸引眼球嘛@_@。

    事實是最近讀《J2EE設計模式》講述表達層模式的那幾章,書中有一個前端控制器+command模式的workflow例子,就琢磨著可以很簡單地擴展成一個MVC框架。花了一個下午改寫了下,對書中所述的理解更為深入。我想這也許對于學習和理解設計模式,以及初次接觸struts等MVC框架的人可能有點幫助。因為整個模型類似于struts,我把它取名叫strutslet^_^。學習性質,切勿認真。

(一)完整的類圖如下:
設計模式:設計自己的MVC框架(圖一)
點擊查看大圖


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.   public ActionForward execute(HttpServletRequest request,
  3.    ServletContext context) {
  4.   String userName=request.getParameter("userName");
  5.   String passWord=request.getParameter("password");
  6.         if(userName.equals("dennis")&&password.equals("123")){
  7.       request.setAttribute("name", name);
  8.       return ActionForward.SUCCESS;  //登陸成功,返回success
  9.         }else
  10.          return ActionForward.FAIL;    //否則,返回fail
  11.  }



 QQread.com 推出各大專業服務器評測 linux服務器的安全性能 SUN服務器 HP服務器 DELL服務器 IBM服務器 聯想服務器 浪潮服務器 曙光服務器 同方服務器 華碩服務器 寶德服務器 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.  }



 QQread.com 推出各大專業服務器評測 Linux服務器的安全性能 SUN服務器 HP服務器 DELL服務器 IBM服務器 聯想服務器 浪潮服務器 曙光服務器 同方服務器 華碩服務器 寶德服務器 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==nulldispatcher_name.equals(""))
  9.    dispatcher_name=Constant.DEFAULT_DISPATCHER;
  10.     
  11.   try {
  12.    Map<String, ActionModel> resources = ConfigUtil.newInstance()  //工具類解析配置文件
  13.      .parse(config_file, context);
  14.    context.setAttribute(Constant.ACTIONS_ATTR, resources);  //存儲在ServletContext中
  15.    log.info("初始化strutslet配置文件成功");
  16.   } catch (Exception e) {
  17.    log.error("初始化strutslet配置文件失敗");
  18.    e.printStackTrace();
  19.   }
  20.   //實例化Dispacher
  21.   try{
  22.    Class c = Class.forName(dispatcher_name);
  23.       Dispatcher dispatcher = (Dispatcher) c.newInstance();
  24.       context.setAttribute(Constant.DISPATCHER_ATTR, dispatcher); //放在ServletContext
  25.       log.info("初始化Dispatcher成功");
  26.   }catch(Exception e) {
  27.     log.error("初始化Dispatcher失敗");
  28.       e.printStackTrace();
  29.   }
  30.   .....


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.   
  10.   request.setAttribute(Constant.REQUEST_ATTR, path);
  11.   Dispatcher dispatcher = (Dispatcher) context.getAttribute(Constant.DISPATCHER_ATTR);
  12.   // make sure we don't cache dynamic data
  13.   response.setHeader("Cache-Control", "no-cache");
  14.   response.setHeader("Pragma", "no-cache");
  15.   // use the dispatcher to find the next page
  16.   String nextPage = dispatcher.getNextPage(request, context);//調用Dispatcher的getNextPage
  17.   // forward control to the view
  18.   RequestDispatcher forwarder = request.getRequestDispatcher("/"
  19.     + nextPage);
  20.   forwarder.forward(request, response);  //轉發頁面
  21.  }



7。最后,web.xml的配置就非常簡單了,配置前端控制器,提供啟動參數(配置文件所在位置,為空就查找/WEB-INF/下面的strutslet-config.xml文件),我們把所有以action結尾的請求都交給FrontController處理:
  1. <servlet>
  2.     <servlet-name>StrutsletController</servlet-name>
  3.     <servlet-class>com.strutslet.core.FrontController</servlet-class>
  4.     <!--  
  5.     <init-param>
  6.          <param-name>config</param-name>
  7.          <param-value>/WEB-INFstrutslet-config.xml</param-value>
  8.     </init-param>
  9.     -->
  10.        <load-on-startup>0</load-on-startup>
  11.   </servlet>
  12.  <servlet-mapping>
  13.     <servlet-name>StrutsletController</servlet-name>
  14.     <url-pattern>*.action</url-pattern>
  15.  </servlet-mapping>


最后,讓我們看看整個框架圖:
設計模式:設計自己的MVC框架(圖二)
點擊查看大圖
 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 资溪县| 收藏| 准格尔旗| 富裕县| 西贡区| 资阳市| 徐闻县| 富裕县| 疏附县| 庆元县| 周至县| 大理市| 双牌县| 福安市| 高唐县| 当涂县| 黄山市| 甘肃省| 翁源县| 开原市| 福泉市| 安庆市| 确山县| 荃湾区| 成安县| 白城市| 水富县| 迁安市| 乌审旗| 凌海市| 青阳县| 都匀市| 定陶县| 阳山县| 邵阳县| 邯郸县| 黎城县| 东辽县| 南昌市| 黎城县| 庆元县|