兩畝三分地

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            17 隨筆 :: 20 文章 :: 2 評論 :: 0 Trackbacks
          發表,瀏覽,回復之后,我們將討論的是刪除和編輯留言。
          因為這個只是一個簡單的留言板,沒有用戶認證之類繁瑣的事情,所以對于編輯和刪除留言,必須輸入
          正確的id號和password;如果在發表或回復留言的時候沒有輸入密碼的話,就不能對留言進行編輯或者刪除。
          這里將寫的EditAction class與之前的有所不同,extends org.apache.struts.actions.DispatchAction;
          1. 在display.jsp的末尾,添加編輯或者刪除的部分
            <html:form action="operate">
                
            <select name="method">
                       
            <option value="edit">編輯</option>
                       
            <option value="delete">刪除</option>
                
            </select>
                 NO:
            <input type="text" name="id" size="10"/>
                 PASS:
            <input type="password" name="password" size="10" maxlength="8"/>
                
            <html:submit value="發送"/>
            </html:form>
            operate是action path,而method則是對應的參數

          2. EditForm bean,由于編輯或者刪除只是針對一條post,所以EditForm bean中的field只需要
            private Post post;
            private List icons;
            其中post就是我們要刪除或者編輯的留言,而icons的集合時用于預處理編輯留言時候的圖片的。

          3. EditAction.java 與之前不同這里Action Path填operate,第二頁parameter填method,與之前jsp中的參數method對應。
              1 public class EditAction extends DispatchAction {
              2 
              3     /* forward name="success" path="" */
              4     private final static String EDIT = "bbs.edit";
              5     private final static String DELETE = "bbs.delete";
              6     private final static String UPDATE = "bbs.update";
              7     private final static String FAILURE = "failure";
              8 
              9     /**
             10      * This is the Struts action method called on
             11      * http:///actionPath?method=myAction1,
             12      * where "method" is the value specified in <action> element : 
             13      * ( <action parameter="method" /> )
             14      */
             15     public ActionForward edit(ActionMapping mapping, ActionForm form,
             16             HttpServletRequest request, HttpServletResponse response) {
             17         EditForm f = (EditForm) form;
             18         String id = (String) request.getParameter("id");
             19         String password = (String) request.getParameter("password");
             20 
             21         if ((password == null|| (password.trim().equals(""))) {
             22             return mapping.findForward(FAILURE);
             23         }
             24 
             25         String sql = "select * from guestbook where id =? and password = ?";
             26         QueryRunner qr = DbHelper.getQueryRunner();
             27         List list = null;
             28         String params[] = {id, password};
             29         try {
             30             list = (List) qr.query(sql, new BeanListHandler(Post.class), params);
             31         } catch (SQLException ex) {
             32             Logger.getLogger(EditAction.class.getName()).log(Level.SEVERE, null, ex);
             33         }
             34 
             35         if (list.size() > 0) {
             36             Post post = (Post) list.get(0);
             37             String content = post.getContent();
             38             content = content.replaceAll("&nbsp;"" ");
             39             content = content.replaceAll("<br>""\n");
             40             post.setContent(content);
             41             f.setPost(post);
             42 
             43             sql = "select * from icon order by id";
             44             List icons = null;
             45             try {
             46                 icons = (List) qr.query(sql, new BeanListHandler(Icon.class));
             47             } catch (SQLException ex) {
             48                 Logger.getLogger(EditAction.class.getName()).log(Level.SEVERE, null, ex);
             49             }
             50             f.setIcons(icons);
             51             return mapping.findForward(EDIT);
             52         } else {
             53             return mapping.findForward(FAILURE);
             54         }
             55     }
             56 
             57     /**
             58      * This is the Struts action method called on
             59      * http:///actionPath?method=myAction2,
             60      * where "method" is the value specified in <action> element : 
             61      * ( <action parameter="method" /> )
             62      */
             63     public ActionForward delete(ActionMapping mapping, ActionForm form,
             64             HttpServletRequest request, HttpServletResponse response) {
             65         EditForm f = (EditForm) form;
             66         String id = (String) request.getParameter("id");
             67         String password = (String) request.getParameter("password");
             68 
             69         if ((password == null|| (password.trim().equals(""))) {
             70             return mapping.findForward(FAILURE);
             71         }
             72 
             73         String sql = "delete from guestbook where (id = ? and password = ?) or replyId = ?";
             74         QueryRunner qr = DbHelper.getQueryRunner();
             75         String params[] = {id, password, id};
             76         int result = 0;
             77         try {
             78             result = qr.update(sql, params);
             79         } catch (SQLException ex) {
             80             Logger.getLogger(EditAction.class.getName()).log(Level.SEVERE, null, ex);
             81         }
             82         if (result != 0) {
             83             return mapping.findForward(DELETE);
             84         } else {
             85             return mapping.findForward(FAILURE);
             86         }
             87     }
             88 
             89     public ActionForward update(ActionMapping mapping, ActionForm form,
             90             HttpServletRequest request, HttpServletResponse response) {
             91         EditForm f = (EditForm) form;
             92         String sql = " update guestbook set name=?,subject=?,email=?,url=?,content=?,iconId=?,password=?,font=? where id = ?";
             93         QueryRunner qr = DbHelper.getQueryRunner();
             94         Post post = f.getPost();
             95         String content = post.getContent();
             96         content = content.replaceAll(" ""&nbsp;");
             97         content = content.replaceAll("\n""<br>");
             98         String params[] = {post.getName(), post.getSubject(), post.getEmail(), post.getUrl(),
             99             content, new Integer(post.getIconId()).toString(), post.getPassword(), post.getFont(), new Integer(post.getId()).toString()};
            100         int result = 0;
            101         try {
            102             result = qr.update(sql, params);
            103         } catch (SQLException ex) {
            104             Logger.getLogger(EditAction.class.getName()).log(Level.SEVERE, null, ex);
            105         }
            106         if (result != 0) {
            107             return mapping.findForward(UPDATE);
            108         } else {
            109             return mapping.findForward(FAILURE);
            110         }
            111     }
            112 }
            注意每個method的名字 對應jsp里的參數值。21-23,69-71行;操作前先檢驗password,是否為空,如果是空的話,不操作。

          4. Edit.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         <form action="">
            15             <input type="button" value="返回前頁" onClick="history.back()">
            16         </form>
            17         <hr width="90%">
            18         <blockquote>
            19             <html:form action="operate">
            20                 <html:hidden property="method" value="update"/>
            21                 <html:hidden name="EditForm" property="post.replyId" />
            22                 <html:hidden name="EditForm" property="post.id"/>
            23                 <table>
            24                     <tbody>
            25                         <tr>
            26                             <td>名字</td>
            27                             <td><html:text property="post.name" /></td>
            28                     </tr>
            29                     <tr>
            30                         <td>郵件</td>
            31                         <td><html:text property="post.email" /></td>
            32                     </tr>
            33                     <tr>
            34                         <td>題目</td>
            35                         <td><html:text property="post.subject" /> <html:submit value="發送"/><html:cancel value="重置"/></td>
            36                     </tr>
            37                     <tr>
            38                         <td colspan="2">正文<br>
            39                     <html:textarea cols="60" rows="8" property="post.content" />
            40                     </td>
            41                     </tr>
            42                     <tr>
            43                         <td>網站</td>
            44                         <td><html:text property="post.url"/></td>
            45                     </tr>
            46                     <tr>
            47                         <td>圖標</td>
            48                         <td>
            49                     <html:select property="post.iconId">
            50                         <logic:iterate id="icon" name="EditForm" property="icons">
            51                             <option value="<bean:write name='icon' property='id'/>" ><bean:write name="icon" property="name"/></option>
            52                         </logic:iterate>
            53                     </html:select>
            54                     </td>
            55                     </tr>
            56                     <tr>
            57                         <td>密碼</td>
            58                         <td><html:password property="post.password"/>(英數8文字內)</td>
            59                     </tr>
            60                     <tr>
            61                         <td>字色</td>
            62                         <td>
            63                     <html:radio property="post.font" value="#800000"><font color="#800000"></font></html:radio>
            64                     <html:radio property="post.font" value="#DF0000"><font color="#DF0000"></font></html:radio>
            65                     <html:radio property="post.font" value="#008040"><font color="#008040"></font></html:radio>
            66                     <html:radio property="post.font" value="#0000FF"><font color="#0000FF"></font></html:radio>
            67                     <html:radio property="post.font" value="#C100C1"><font color="#C100C1"></font></html:radio>
            68                     <html:radio property="post.font" value="#FF80C0"><font color="#FF80C0"></font></html:radio>
            69                     <html:radio property="post.font" value="#FF8040"><font color="#FF8040"></font></html:radio>
            70                     <html:radio property="post.font" value="#000080"><font color="#000080"></font></html:radio>
            71                     </td>
            72                     </tr>
            73                     </tbody>
            74                 </table>
            75             </html:form>
            76         </blockquote>
            77     </body>
            78 </html>
            79 
            與之前回復,發表的網頁基本相同。

          5. 測試用的result.jsp頁面,現在2個Form用了,所以要改一下
            <html>
                
            <head>
                    
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                    
            <title>留言板</title>
                
            </head>
                
            <body>
                    
            <logic:notEmpty name="NewForm">
                        
            <h1><bean:write name="NewForm" property="result" /></h1>
                    
            </logic:notEmpty>
                    
            <logic:notEmpty name="EditForm">
                        
            <h1>失敗</h1>
                    
            </logic:notEmpty>
                
            </body>
            </html>

          6. 在struts-config.xml中添加對應的forward path
                    <forward name="bbs.edit" path="/edit.jsp" />
                    
            <forward name="bbs.delete" path="/list.do" />
                    
            <forward name="bbs.update" path="/list.do" />
                    
            <forward name="failure" path="/result.jsp"/>
          寫完收工
          posted on 2009-10-29 17:22 Chucky 閱讀(244) 評論(0)  編輯  收藏 所屬分類: BBS Struts項目
          主站蜘蛛池模板: 宜春市| 岚皋县| 伽师县| 大邑县| 聂拉木县| 万山特区| 丹寨县| 墨竹工卡县| 吉木萨尔县| 奎屯市| 长宁区| 合川市| 深水埗区| 陈巴尔虎旗| 互助| 含山县| 梅州市| 尖扎县| 板桥市| 霍城县| 芦山县| 凤冈县| 冷水江市| 双桥区| 广德县| 丰宁| 建始县| 湖南省| 泰宁县| 葵青区| 庆云县| 承德市| 凤山县| 阳泉市| 清镇市| 仪陇县| 彩票| 巩留县| 兰溪市| 广南县| 呼图壁县|