dyerac  
          dyerac In Java
          公告

          日歷
          <2006年12月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456
          統(tǒng)計
          • 隨筆 - 36
          • 文章 - 10
          • 評論 - 94
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿(5)

          隨筆分類(49)

          隨筆檔案(36)

          文章分類(11)

          文章檔案(10)

          相冊

          dyerac

          搜索

          •  

          積分與排名

          • 積分 - 79314
          • 排名 - 705

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

           
          ??? JSF作為如今JavaEE標準之一,基于組件的設(shè)計為Java的web開發(fā)帶來了極大的便利性。然而在設(shè)計上,JSF拋棄了傳統(tǒng)的GET請求方式,所有JSF的表單都已post方式提交。這樣雖然在安全性等方面有一定好處,但是也帶來了一些不便,比如,由JSF開發(fā)的web應(yīng)用難以為每個單元定位。
          ?? 上述單元定位的意思是,比如你開發(fā)了一個project.faces用于動態(tài)顯示project的內(nèi)容。由于是post提交表單,那么一個叫dollyCal的項目和一個叫nirvana的項目將同樣顯示為project.faces,這樣每次用戶都需要通過導(dǎo)航瀏覽自己需要的項目,而不能通過對固定網(wǎng)頁的收藏來一次性瀏覽。
          ??? 解決上述問題可以通過filter來模擬get請求。
          ???以下還是以dollyCal舉例:
          ?? GET請求下url形式為project.faces?name=dollyCal
          ??? 則通過filter攔截該請求,取出dollyCal字段,然后調(diào)用FacesContext,初始化相應(yīng)的managed-bean,從數(shù)據(jù)庫中讀出dollyCal的相關(guān)信息填充到managed-bean中,再通過dispatcher導(dǎo)航到相應(yīng)頁面即可。這樣用戶可以project.faces?name=dollyCal形式輕松訪問一個項目,方便其收藏和訂閱
          ???
          if?(request?instanceof?HttpServletRequest)?{
          ????????????HttpServletRequest?r?
          =?(HttpServletRequest)?request;
          ????????????String?s?
          =?r.getQueryString();
          ????????????
          if?(s?!=?null?&&?s.startsWith("project=")?&&?s.length()?>?8)?{
          ????????????????String?name?
          =?s.substring(8);
          ????????????????
          if?(!PersistenceUtil.validateProject(name))?{
          ????????????????????FacesContext?fcg?
          =?getFacesContext(request,response);
          ????????????????????ProjectViewBean?pjv
          =(ProjectViewBean)fcg.getApplication().getVariableResolver().resolveVariable(fcg,"pjv");
          ????????????????????
          try?{
          ????????????????????????ProjectBean?p
          =PersistenceUtil.getProjectByName(name);
          ????????????????????????pjv.setProject(p);
          ????????????????????}
          ?catch?(Exception?e)?{
          ????????????????????????
          //?TODO?Auto-generated?catch?block
          ????????????????????????e.printStackTrace();
          ????????????????????}

          ????????????????????RequestDispatcher?rd
          =request.getRequestDispatcher("/projectView.faces");
          ????????????????????rd.forward(request,?response);
          ????????????????}

          ????????????}

          ??? 唯一需要注意的問題是,filter實際上處于JSF的管理區(qū)域之外,因此不能調(diào)用FacesContext.getCurrentInstance方法來獲得JSF上下文,而需要通過request, response來創(chuàng)建當前JSF上下文。再從該上下文中查出managed-bean,進行數(shù)據(jù)填充,
          ? 以下是完整代碼,說明如下:
          ?? ProjectViewBean是projectView.faces頁面的managed-bean, 首先解析url中要查詢的項目名稱,從數(shù)據(jù)庫中讀出相應(yīng)信息,再從上下文中查詢出該bean,填充信息即可。這樣再訪問該頁面時就會得到想要的結(jié)果。

          package?org.myibm;

          import?java.io.IOException;

          import?javax.faces.FactoryFinder;
          import?javax.faces.context.FacesContext;
          import?javax.faces.context.FacesContextFactory;
          import?javax.faces.lifecycle.Lifecycle;
          import?javax.faces.lifecycle.LifecycleFactory;
          import?javax.servlet.Filter;
          import?javax.servlet.FilterChain;
          import?javax.servlet.FilterConfig;
          import?javax.servlet.RequestDispatcher;
          import?javax.servlet.ServletContext;
          import?javax.servlet.ServletException;
          import?javax.servlet.ServletRequest;
          import?javax.servlet.ServletResponse;
          import?javax.servlet.http.HttpServletRequest;

          import?org.myibm.beans.ProjectViewBean;
          import?org.myibm.persistence.bean.ProjectBean;
          import?org.myibm.utils.PersistenceUtil;

          public?class?ParserFilter?implements?Filter?{

          ????
          private?FilterConfig?filterConfig?=?null;

          ????
          public?void?destroy()?{
          ????????
          //?TODO?Auto-generated?method?stub
          ????????filterConfig?=?null;
          ????}


          ????
          public?void?doFilter(ServletRequest?request,?ServletResponse?response,
          ????????????FilterChain?chain)?
          throws?IOException,?ServletException?{
          ????????
          //?TODO?Auto-generated?method?stub
          ????????if?(request?instanceof?HttpServletRequest)?{
          ????????????HttpServletRequest?r?
          =?(HttpServletRequest)?request;
          ????????????String?s?
          =?r.getQueryString();
          ????????????
          if?(s?!=?null?&&?s.startsWith("project=")?&&?s.length()?>?8)?{
          ????????????????String?name?
          =?s.substring(8);
          ????????????????
          if?(!PersistenceUtil.validateProject(name))?{
          ????????????????????FacesContext?fcg?
          =?getFacesContext(request,response);
          ????????????????????ProjectViewBean?pjv
          =(ProjectViewBean)fcg.getApplication().getVariableResolver().resolveVariable(fcg,"pjv");
          ????????????????????
          try?{
          ????????????????????????ProjectBean?p
          =PersistenceUtil.getProjectByName(name);
          ????????????????????????pjv.setProject(p);
          ????????????????????}
          ?catch?(Exception?e)?{
          ????????????????????????
          //?TODO?Auto-generated?catch?block
          ????????????????????????e.printStackTrace();
          ????????????????????}

          ????????????????????RequestDispatcher?rd
          =request.getRequestDispatcher("/projectView.faces");
          ????????????????????rd.forward(request,?response);
          ????????????????}

          ????????????}

          ????????}

          ????????
          try?{
          ????????????
          //?System.out.print("filter?begins!");
          ????????????chain.doFilter(request,?response);
          ????????????
          //?System.out.print("filter?works!");
          ????????}
          ?finally?{

          ????????}

          ????}


          ????
          public?void?init(FilterConfig?arg0)?throws?ServletException?{
          ????????
          //?TODO?Auto-generated?method?stub
          ????????this.filterConfig?=?arg0;
          ????}


          ????
          private?abstract?static?class?InnerFacesContext?extends?FacesContext?{
          ????????
          protected?static?void?setFacesContextAsCurrentInstance(
          ????????????????FacesContext?facesContext)?
          {
          ????????????FacesContext.setCurrentInstance(facesContext);
          ????????}

          ????}


          ????
          private?FacesContext?getFacesContext(ServletRequest?request,
          ????????????ServletResponse?response)?
          {
          ????????
          //?Try?to?get?it?first
          ????????FacesContext?facesContext?=?FacesContext.getCurrentInstance();
          ????????
          if?(facesContext?!=?null)
          ????????????
          return?facesContext;

          ????????FacesContextFactory?contextFactory?
          =?(FacesContextFactory)?FactoryFinder
          ????????????????.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
          ????????LifecycleFactory?lifecycleFactory?
          =?(LifecycleFactory)?FactoryFinder
          ????????????????.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
          ????????Lifecycle?lifecycle?
          =?lifecycleFactory
          ????????????????.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

          ????????
          //?Either?set?a?private?member?servletContext?=
          ????????
          //?filterConfig.getServletContext();
          ????????
          //?in?you?filter?init()?method?or?set?it?here?like?this:
          ????????
          //?ServletContext?servletContext?=
          ????????
          //?((HttpServletRequest)request).getSession().getServletContext();
          ????????
          //?Note?that?the?above?line?would?fail?if?you?are?using?any?other
          ????????
          //?protocol?than?http
          ????????ServletContext?servletContext?=?((HttpServletRequest)?request)
          ????????????????.getSession().getServletContext();

          ????????
          //?Doesn't?set?this?instance?as?the?current?instance?of
          ????????
          //?FacesContext.getCurrentInstance
          ????????facesContext?=?contextFactory.getFacesContext(servletContext,?request,
          ????????????????response,?lifecycle);

          ????????
          //?Set?using?our?inner?class
          ????????InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);

          ????????
          //?set?a?new?viewRoot,?otherwise?context.getViewRoot?returns?null
          ????????
          //?UIViewRoot?view?=
          ????????
          //?facesContext.getApplication().getViewHandler().createView(facesContext,
          ????????
          //?"yourOwnID");
          ????????
          //?facesContext.setViewRoot(view);

          ????????
          return?facesContext;
          ????}

          }

          posted on 2006-12-02 02:33 dyerac in java... 閱讀(2963) 評論(4)  編輯  收藏
          評論:
          • # re: 在JSF中用Filter模擬Get請求  逃兵 Posted @ 2006-12-02 13:05
            搞不懂,我經(jīng)常在jsf中,經(jīng)常以project.faces?name=dollyCal這種形式即用url來傳輸參數(shù),后臺用String name= (String) FacesContext.getCurrentInstance()
            .getExternalContext().getRequestParameterMap().get("name");來獲取"name"的參數(shù),
            也不需要先鏈接其他頁面再導(dǎo)航過來。
            有必要寫這么多余而又復(fù)雜的代碼嗎?  回復(fù)  更多評論   

          • # re: 在JSF中用Filter模擬Get請求  阿蛋 Posted @ 2008-02-22 17:24
            樓上兄弟你就不知道了...

            在你自己的 filter 中 FacesContext.getCurrentInstance() 返回的是 null...  回復(fù)  更多評論   

          • # # re: 在JSF中用Filter模擬Get請求  阿蛋 Posted @ 2008-02-22 17:25
            啊哈,搞錯了!!!! 抱歉了..  回復(fù)  更多評論   

          • # re: 在JSF中用Filter模擬Get請求  hongingr Posted @ 2014-05-26 10:36
            我自己在web.xml中配了filter后就不能正常運行了。。。請問能否把前臺必須的代碼和配置文件也放出來呢?因為是初學者很困擾,感激不盡~~  回復(fù)  更多評論   


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


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © dyerac in java... Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 和硕县| 北海市| 福海县| 冕宁县| 马鞍山市| 天水市| 萝北县| 阳春市| 金昌市| 饶河县| 玉屏| 福泉市| 洪湖市| 内丘县| 苗栗县| 汝南县| 卓资县| 化州市| 哈密市| 横峰县| 林芝县| 沧州市| 宁远县| 易门县| 通州区| 博野县| 庐江县| 安乡县| 美姑县| 镇康县| 彰化县| 山阳县| 玛沁县| 周至县| 洱源县| 安远县| 广德县| 庄浪县| 桂平市| 绍兴县| 辽源市|