两亩三分地

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

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


网站导航: