兩畝三分地

            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 閱讀(147) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 邯郸市| 榆中县| 新和县| 梅河口市| 怀集县| 余干县| 富民县| 红桥区| 漠河县| 隆德县| 长兴县| 汤阴县| 湖南省| 涟源市| 衡东县| 淅川县| 桂阳县| 进贤县| 临城县| 巴马| 巴中市| 四平市| 南宫市| 洪江市| 庐江县| 庆城县| 汉川市| 旌德县| 广河县| 朝阳市| 息烽县| 孟连| 淄博市| 贺州市| 迁安市| 安庆市| 安岳县| 荔波县| 塘沽区| 靖边县| 图片|