两亩三分地

  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 阅读(230) 评论(0)  编辑  收藏 所属分类: BBS Struts项目

只有注册用户登录后才能发表评论。


网站导航: