兩畝三分地

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            17 隨筆 :: 20 文章 :: 2 評論 :: 0 Trackbacks
          既然我們已經成功完成了第一個ActionForm Bean和與之相關的Action Class;后面的專題中將不再詳細的去寫與他們相關的開發步驟了。
          現在我們開始寫留言板的主體部分,即對整個留言就行瀏覽。因為每個post都有一個replyId字段,用來對應其所回復的留言id;如果這個
          replyId等于-1的話,即該留言沒有對應的回復。好了還是先從這個JSP頁面寫起。
          1. display.jsp
             1 <%@page contentType="text/html" pageEncoding="UTF-8"%>
             2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
             3     "http://www.w3.org/TR/html4/loose.dtd">
             4 <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
             5 <%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
             6 <%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
             7 
             8 <html>
             9     <head>
            10         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            11         <title>留言板</title>
            12     </head>
            13     <body>
            14         <logic:iterate name="ListForm" property="topics" id="topic">
            15             <table>
            16                 <tr>
            17                     <td>
            18                         <table>
            19                             <tr>
            20                                 <td>
            21                                     <bean:write name="topic" property="post.subject"/> 留言者:<bean:write name="topic" property="post.name" /> 留言日:<bean:write name="topic" property="post.date" format="yyyy/MM/dd(E) HH:mm" />  No.<bean:write name="topic" property="post.id" />
            22                                 </td>
            23                             </tr>
            24                         </table>
            25                         <table>
            26                             <tr>
            27                                 <td>
            28                                     <img src="<bean:write name='topic' property='post.icon'/>" alt=""/>
            29                                 </td>
            30                                 <td>
            31                                     <font color="<bean:write name='topic' property='post.font'/>">
            32                                         <bean:write name="topic" property="post.content" filter="false" />
            33                                     </font>
            34                                 </td>
            35                             </tr>
            36                         </table>
            37                         <logic:iterate name="topic" property="replies" id="reply">
            38                             <hr>
            39                             <table>
            40                                 <tr>
            41                                     <td><bean:write name="reply" property="subject"/><bean:write name="reply" property="name"/> - <bean:write name="reply" property="date" format="yyyy         /MM/dd(E) HH:mm" /> No.<bean:write name="reply" property="id"/></td>
            42                                 </tr>
            43                             </table>
            44                             <table>
            45                                 <tr>
            46                                     <td>
            47                                         <img src="<bean:write name='reply' property='icon' />" alt=""/>
            48                                     </td>
            49                                     <td><font color="<bean:write name='reply' property='font'/>"><bean:write name="reply" property="content" filter="false"/></font></td>
            50                                 </tr>
            51                             </table>
            52                         </logic:iterate>
            53                         <hr>
            54                     </td>
            55                 </tr>
            56             </table>
            57         </logic:iterate>
            58     </body>
            59 </html>
            60 
            可以注意到在網頁里面,多了一個<logic:iterator>的tag,這個標記是對內部指定的數據就行遍歷(循環)。


          2. 根據這個jsp網頁,對應的ActionForm bean很簡單
            ListForm.java中只需要一個字段,用于收集所有的留言
            public class ListForm extends org.apache.struts.action.ActionForm {
                
            private List topics;

                
            public List getTopics() {
                    
            return topics;
                }

                
            public void setTopics(List topics) {
                    
            this.topics = topics;
                }
                
            }


          3. 創建對應的Action class。
             1 public class ListAction extends org.apache.struts.action.Action {
             2 
             3     /* forward name="success" path="" */
             4     private static final String SUCCESS = "bbs.list";
             5 
             6     /**
             7      * This is the action called from the Struts framework.
             8      * @param mapping The ActionMapping used to select this instance.
             9      * @param form The optional ActionForm bean for this request.
            10      * @param request The HTTP Request we are processing.
            11      * @param response The HTTP Response we are processing.
            12      * @throws java.lang.Exception
            13      * @return
            14      */
            15     @Override
            16     public ActionForward execute(ActionMapping mapping, ActionForm form,
            17             HttpServletRequest request, HttpServletResponse response) {
            18         ListForm f = (ListForm) form;
            19         String sql;
            20         QueryRunner qr = DbHelper.getQueryRunner();
            21         List list = new ArrayList();
            22         List posts = null;
            23 
            24         // default replyId = -1 means this post is the topic
            25         sql = "select g.id as id, g.name as name,subject,content,email,url,replyId,iconId,i.src as icon,lastReplyTime,date,font from guestbook g, icon i " + " where replyId = -1 and i.id = iconId order by lastReplyTime desc ";
            26         try {
            27             posts = (List) qr.query(sql, new BeanListHandler(Post.class));
            28         } catch (SQLException ex) {
            29             Logger.getLogger(ListAction.class.getName()).log(Level.SEVERE, null, ex);
            30         }
            31         //according to topics.id, querying all related post;
            32         for (int i = 0; i < posts.size(); i++) {
            33             Post post = (Post) posts.get(i);
            34             List replies = null;
            35             sql = "select g.id as id, g.name as name, subject,content,email,url,replyId,iconId,i.src as icon,lastReplyTime,date,font from guestbook g, icon i " + " where replyId = " + post.getId() + " and i.id = iconId order by id";
            36             try {
            37                 replies = (List) qr.query(sql, new BeanListHandler(Post.class));
            38             } catch (SQLException ex) {
            39                 Logger.getLogger(ListAction.class.getName()).log(Level.SEVERE, null, ex);
            40             }
            41             list.add(new Topic(post,replies));
            42         }
            43         f.setTopics(list);
            44         return mapping.findForward(SUCCESS);
            45     }
            46 }
            27行先查詢主題留言,32-42行對每個主題留言查詢與之相關的回復,41行 創建一個topic對象,添加進集合;43行對listForm的topics賦值。

          4. 相關的struts-config.xml的聲明如下
             1 <form-beans>
             2         <form-bean name="ListForm" type="com.bbs.struts.form.ListForm"/>
             3         <form-bean name="NewForm" type="com.bbs.struts.form.NewForm"/>
             4     
             5 </form-beans>
             6 
             7 
             8 <global-forwards>
             9         <forward name="bbs.post" path="/result.jsp"/>
            10         <forward name="bbs.list" path="/display.jsp"/>
            11         <forward name="welcome"  path="/Welcome.do"/>
            12 </global-forwards>
            13 
            14 <action-mappings>
            15         <action input="/display.jsp" name="ListForm" path="/list" scope="request" type="com.bbs.struts.action.ListAction" validate="false"/>
            16         <action input="/post.jsp" name="NewForm" path="/post" scope="request" type="com.bbs.struts.action.PostAction"/>
            17         <action path="/Welcome" forward="/welcomeStruts.jsp"/>
            18 </action-mappings>

          5. 測試
            deploy的應用,瀏覽器中輸入http://localhost:8080/BBS/list.do,測試正確(暫時不會有回復出現,下一節我們將討論留言回復的問題)。


          posted on 2009-10-23 17:40 Chucky 閱讀(282) 評論(0)  編輯  收藏 所屬分類: BBS Struts項目
          主站蜘蛛池模板: 调兵山市| 定州市| 彭阳县| 虞城县| 安庆市| 山东| 东丰县| 申扎县| 革吉县| 朝阳县| 佛山市| 宜阳县| 雷州市| 义马市| 龙口市| 苏州市| 通州区| 石棉县| 自贡市| 兴海县| 宜章县| 满洲里市| 堆龙德庆县| 枣庄市| 上犹县| 花莲市| 莒南县| 措勤县| 遂川县| 古浪县| 岳阳县| 大埔区| 牡丹江市| 托克逊县| 汶川县| 乃东县| 元阳县| 自贡市| 彝良县| 洛扎县| 兴仁县|