好·色之徒
          已經(jīng)轉(zhuǎn)移到 好·色之徒--我的博客、我的生活

          為了更好的幫助理解Model2結(jié)構(gòu),這里先介紹一下有關(guān)基礎(chǔ)的一些知識(shí)
          這是2、3年前整理出來的東西,希望現(xiàn)在還能有所幫助

          概要:Front Controller(模式)

          說明:
          講解struts具體例子之前,先說明一下Front Controller模式(前端控制器模式)。struts就是它的具體實(shí)現(xiàn),得以擴(kuò)展,并且是Model2結(jié)構(gòu)的完整實(shí)現(xiàn)。

          Model2結(jié)構(gòu)通過一個(gè)控制組件來接受請(qǐng)求,這個(gè)控制組件本身可以為請(qǐng)求提供服務(wù),也可以把這項(xiàng)任務(wù)分配給其他一些組件。生成響應(yīng)信息的任務(wù)接著被分配給適當(dāng)?shù)囊晥D組件。實(shí)現(xiàn)這種要求的方式很多,F(xiàn)ront Controller就是其中一種。

          Front Controller 實(shí)現(xiàn)前端控制器可以采取不同的策略,一種典型的策略是使用一個(gè)servlet
          問題:如果把所有的功能集中起來會(huì)出現(xiàn)一個(gè)大而臃腫的控制器組件,它將負(fù)責(zé)整個(gè)Web的相關(guān)處理。
          解決:辦法之一是使用多個(gè)前端控制器,分別負(fù)責(zé)某一方面的處理任務(wù)
          另一種解決方案是使用Command and Controller(命令與控制器)(稍候介紹)

          下面是這種Front Controller模式示例程序框架實(shí)現(xiàn):
          public class FrontController extends HttpServlet {
          protected void processRequest(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {
          RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextView);
          dispatcher.forward(req, res);
          }

          protected void doGet(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {
          processRequest(req, res);
          }

          protected void doPost(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {
          processRequest(req, res);
          }
          }

          在這里,它只是HttpServlet一個(gè)簡單的擴(kuò)充,默認(rèn)實(shí)現(xiàn)了doGet()和doPost()方法,這兩個(gè)方法負(fù)責(zé)把請(qǐng)求分配給另一個(gè)叫processRequest()的方法。這是為了保證不論發(fā)出什么請(qǐng)求,前端控制器都能為之提供服務(wù)。
          在processRequest()的方法中我們省去主要部分,但實(shí)際上前端控制器將先執(zhí)行與請(qǐng)求關(guān)聯(lián)的處理,然后再把它分配給一個(gè)視圖組件來生成相關(guān)的響應(yīng)信息。視圖組件一般是JSP頁面。控制器完成了自己的業(yè)務(wù)邏輯之后,它就可以通過RequestDispatcher把處理分配給一個(gè)JSP。
          到此你可能會(huì)提出一個(gè)問題。如果控制器要負(fù)責(zé)處理所有的請(qǐng)求,那么它怎么知道各請(qǐng)求都是什么以及如何處理呢?(Command and Controller,上面問題所提到的,現(xiàn)在開始講解)

          Command and Controller策略
          在Command and Controller策略中,處理每個(gè)具體請(qǐng)求的邏輯被轉(zhuǎn)移到了一個(gè)單獨(dú)的組件中。

          Command and Controller 這些額外組件各表示一個(gè)具體的命令(或操作,既action),并且組件中封裝著執(zhí)行相關(guān)操作的邏輯。
          前端控制器把對(duì)請(qǐng)求的處理分配給適當(dāng)?shù)拿罱M件。
          操作類
          為了實(shí)現(xiàn)Command and Controller策略,首先需要定義位于控制器和操作組件之間的接口。
          public abstract class Action {
          public abstract String process(HttpServletRequest req, HttpServletResponse res);
          }

          關(guān)于請(qǐng)求類型的通信
          這里的處理如下:FrontController Servlet和URI/controller/*之間定義了一個(gè)映射

          FrontController
          com.wxhx.controller.FrontController


          FrontController
          /controller/*


          為了從附加路徑信息中獲得的字符串映射到一個(gè)特定的操作實(shí)例上,當(dāng)然,同樣有很多策略可供選擇,其中最靈活的就是把這種映射外部化,例如可以通過一個(gè)XML文件。
          為了簡單起見,我們?cè)谶@里建立一個(gè)單獨(dú)的組件,用于封裝這種映射。
          public class ActionHelper {
          private static HashMap actions = new HashMap();
          static {
          actions.put("Login", "com.sample.LoginAction");
          }
          public static Action getAction(String name) {
          Action action = null;
          try {
          Class c = Class.forName((String) actions.get(name));
          action = (Action) c.newInstance();
          } catch (Exception e) {
          e.printStackTrace();
          }
          return action;
          }
          }
          好了,以上就是要實(shí)現(xiàn)的一個(gè)簡單的框架類FrontController,Action以及輔助的ActionHelper類

          下一步是把所有這些處理插入到FrontController的processRequest方法中,這將是Web應(yīng)用程序中所有請(qǐng)求的單一入口:
          public class FrontController extends HttpServlet {
          protected void processRequest(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {

          String actionName =req.getPathInfo().substring(1);
          Action action =ActionHelper.getAction(actionName);

          String nextView =action.process(req, res);
          RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextView);
          dispatcher.forward(req, res);
          }

          protected void doGet(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {
          processRequest(req, res);
          }

          protected void doPost(HttpServletRequest req, HttpServletResponse res)
          throws ServletException, IOException {
          processRequest(req, res);
          }
          }

          不借助任何web框架結(jié)構(gòu),構(gòu)建自己的MVC應(yīng)用程序(2)?? 有個(gè)論壇

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          posts - 39, comments - 91, trackbacks - 0, articles - 1

          Copyright © 城市劣人

          好·色之徒
          主站蜘蛛池模板: 洛阳市| 景洪市| 嘉鱼县| 桃园市| 老河口市| 大兴区| 凤凰县| 米易县| 林芝县| 莫力| 同德县| 扎赉特旗| 嫩江县| 和龙市| 修文县| 连城县| 天祝| 和政县| 永顺县| 墨玉县| 泗水县| 崇左市| 盘山县| 东平县| 华宁县| 沈阳市| 浦东新区| 蒲江县| 资中县| 敦煌市| 阜康市| 丰台区| 锡林浩特市| 兴海县| 石阡县| 淅川县| 平阳县| 高平市| 图木舒克市| 清水县| 隆昌县|