兩畝三分地

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            17 隨筆 :: 20 文章 :: 2 評論 :: 0 Trackbacks
          到11節基本的東西都已經實現了,剩下的都是一點細節問題;比如由于注冊用戶和普通瀏覽用戶權限不同,所以大家看的也有所不同;
          在項目設計的時候,普通用戶可以看博客,發表評論;為了避免不必要的麻煩,將普通用戶的操作特別的寫一個servlet,這樣也比較容易控制。
          HomeServlet主要就是負責這個任務。
          在HomeServlet里面主要就是兩個方法,為了配合以后的頁面設計,相對之前的BlogServlet等等,相對復雜一點(其實還好)。

          讓我們來看看這個Sql語句的作用
          sql= select b.id as id,b.title as title,b.content as content,b.date as date,c.name as category,categoryId,comments from
          (
          select blog.id as id ,blog.title as title,blog.category_id as categoryId,count(comment.blog_id) as comments,blog.content as content,blog.date as date from blog
          left
           join comment on blog.id = comment.blog_id group by blog.id) as b, category c
          where categoryId = c.id
          order by date desc;
          首先
          select blog.id as id ,blog.title as title,blog.category_id as categoryId,count(comment.blog_id) as comments,blog.content as content,blog.date as date from blog
          left join comment on blog.id = comment.blog_id group by blog.id
          comment表中對blog_id一致的記錄做統計,與blog表左連接;
          然后再blog新表與category根據categoryId做連接。

          然后修改一下Blog類,添加一個comments屬性(記錄此blog對象所包含的評論數目)。

          2.對blog.content做下處理,在顯示所有博文的時候,對blog.content的內容做一個簡報。
          很簡單的一個getBriefContent()方法:
          public String getBriefContent() {
                  StringBuffer briefContent 
          = new StringBuffer(content);
                  
          int length = 200;
                  
          if (briefContent.length() < length) {
                      length 
          = briefContent.length();
                  }
                  briefContent 
          = briefContent.delete(length, briefContent.length());

                  
          //filter html mark;
                  briefContent.append(" ..");
                  
          return briefContent.toString();
              }
          限定200字符長度,如果博文內容多于200字符,那么取前200字符;否則直接貼上博文內容。

          3.對blog.content另一個修改。因為寫博文的時候,可能所選用的字體啊什么每次都不同,以至于在瀏覽所有博文的時候會很亂;
          所以以下doFilter(StringBuffer sb)方法,主要對文字進行過濾,將充滿HTML MARK的文章過濾一下。
          private StringBuffer doFilter(StringBuffer source) {
                  StringBuffer header 
          = new StringBuffer(source);
                  
          while((header.indexOf("<")!=-1)&&(header.indexOf(">")!=-1)){
                     
          int startPos = header.indexOf("<");
                     
          int endPos = header.indexOf(">");
                      header 
          = header.delete(startPos, endPos+1);
                  }
                  
          return header;
              }
          因此getBriefContent()方法也要插上一句(Line 3):
           1 public String getBriefContent() {
           2         StringBuffer briefContent = new StringBuffer(content);
           3         briefContent = doFilter(briefContent);
           4         int length = 200;
           5         if (briefContent.length() < length) {
           6             length = briefContent.length();
           7         }
           8         briefContent = briefContent.delete(length, briefContent.length());
           9 
          10         //filter html mark;
          11         briefContent.append(" ..");
          12         return briefContent.toString();
          13     }



          posted on 2009-10-05 16:26 Chucky 閱讀(149) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 澜沧| 英山县| 兴仁县| 固原市| 茌平县| 嘉善县| 石楼县| 仪陇县| 和政县| 东阳市| 寿宁县| 伊川县| 安乡县| 桃源县| 清远市| 岳池县| 定陶县| 利川市| 昌邑市| 鲁山县| 乌拉特中旗| 东乡| 富平县| 乡城县| 滨海县| 保亭| 汉源县| 盐山县| 绵阳市| 临安市| 安宁市| 平谷区| 濮阳市| 沙河市| 确山县| 犍为县| 多伦县| 衡南县| 梓潼县| 达州市| 南宁市|