<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    dyerac  
    dyerac In Java
    公告

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

    導航

    常用鏈接

    留言簿(5)

    隨筆分類(49)

    隨筆檔案(36)

    文章分類(11)

    文章檔案(10)

    相冊

    dyerac

    搜索

    •  

    積分與排名

    • 積分 - 79300
    • 排名 - 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,這樣每次用戶都需要通過導航瀏覽自己需要的項目,而不能通過對固定網(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導航到相應(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... 閱讀(2962) 評論(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ù),
      也不需要先鏈接其他頁面再導航過來。
      有必要寫這么多余而又復雜的代碼嗎?  回復  更多評論   

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

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

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

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


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


    網(wǎng)站導航:
     
     
    Copyright © dyerac in java... Powered by: 博客園 模板提供:滬江博客
    主站蜘蛛池模板: 成人无码区免费A∨直播| 亚洲高清免费在线观看| 亚洲成熟xxxxx电影| 色影音免费色资源| 爱爱帝国亚洲一区二区三区| 中文字幕一精品亚洲无线一区| 久久久久久夜精品精品免费啦| 亚洲最大无码中文字幕| 亚洲精品国产电影| 91香焦国产线观看看免费| 亚洲欧美综合精品成人导航| 337p日本欧洲亚洲大胆裸体艺术| 手机看黄av免费网址| 一区二区视频免费观看| 亚洲午夜一区二区三区| 国产亚洲精久久久久久无码AV| 无码人妻久久一区二区三区免费丨| 男人和女人高潮免费网站| 亚洲妓女综合网99| 中文字幕一精品亚洲无线一区| 日韩欧美一区二区三区免费观看| aa在线免费观看| 亚洲一区二区三区在线观看网站| 亚洲av无码国产精品夜色午夜| 哒哒哒免费视频观看在线www| 最近免费中文字幕mv电影| 免费无码又爽又黄又刺激网站| 亚洲另类精品xxxx人妖| 日本亚洲成高清一区二区三区| 亚洲A丁香五香天堂网| 中国在线观看免费国语版| 国产精品区免费视频| 亚州**色毛片免费观看| 亚洲AV无码专区在线观看成人| 亚洲男人天堂2017| 伊人婷婷综合缴情亚洲五月| 国产高清免费的视频| 黄页网站在线观看免费高清| 久久永久免费人妻精品| 国产精品高清免费网站| 免费视频精品一区二区|