兩畝三分地

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            17 隨筆 :: 20 文章 :: 2 評論 :: 0 Trackbacks
          PostAction主要是處理留言添加工作。
          1. 和創建NewForm bean一樣,點擊Source Packages > com.bbs.struts,右鍵New > Java Package... 創建一個com.bbs.struts.action的package用于存放所有action類;

          2. 右鍵com.bbs.struts.action, New > Struts Action...如果New菜單里沒有的話,選擇other...,categories里選擇
            Struts,File types一欄里選擇Struts Action ...;

          3. 在New Struts Action面板里面,Class Name填PostAction;Action Path 填/post;按Next


          4. 按照下面的設置,完成對Action的設置


          5. 在struts-config.xml的文檔中,IDE自動添加了對PostForm的聲明。
            <action-mappings>
                    
            <action input="/post.jsp" name="NewForm" path="/post" scope="request" type="com.bbs.struts.action.PostAction"/>
                    
            <action path="/Welcome" forward="/welcomeStruts.jsp"/>
            </action-mappings>

          6. 右邊的Source Editor中,新建的PostAction.java已經打開了;接下來我們要把記錄在NewForm bean里的數據保存進數據庫,為了測試的需要,在添加數據以后,將添加成功與否的結果顯示在result.jsp上面。
             1     public ActionForward execute(ActionMapping mapping, ActionForm form,
             2             HttpServletRequest request, HttpServletResponse response) {
             3         NewForm f = (NewForm) form;
             4         String sql = "insert into guestbook (name,subject,email,url,content,iconId,password,font,replyId,date,lastReplyTime) " +
             5                 " values(?,?,?,?,?,?,?,?,-1,now(),now())";
             6 
             7         String content = f.getContent();
             8         content = content.replaceAll(" ""&nbsp;");
             9         content = content.replaceAll("\n""<br>");
            10 
            11         String params[] = {f.getName(), f.getSubject(), f.getEmail(), f.getUrl(), content, new Integer(f.getIconId()).toString(), f.getPassword(), f.getFont()};
            12 
            13         QueryRunner qr = DbHelper.getQueryRunner();
            14 
            15         String result = null;
            16         try {
            17             if (qr.update(sql, params) == 1){
            18                 result = "更新成功";
            19             }else{
            20                 result = "更新失敗";
            21             }
            22         } catch (SQLException ex) {
            23             Logger.getLogger(PostAction.class.getName()).log(Level.SEVERE, null, ex);
            24         }
            25         f.setResult(result);
            26         return mapping.findForward(SUCCESS);
            27     }
            7,8,9行主要是將content里的空格和回車符號轉成html中所對應的空格和回車。

          7. 在PostAction.java,IDE自動給我們設置了一個 private static final String SUCCESS = "success"; 這是為action forward所設置的forward標志,success的名稱我們可以自己取,比如我們可以把它改成bbs.post。關鍵是在結束對這個action class的編程以后,我們需要在struts-config.xml中添加forward聲明。在source editor中打開struts-config.xml,右鍵菜單Struts > Add Forward,在Add Forward的面板里如下設置,按Add完成添加。

          按F6 運行項目,瀏覽器輸入:http://localhost:8080/BBS/post.jsp對程序進行測試。如果添加成功的話,下一頁將有更新成功的字樣。
          posted on 2009-10-23 00:31 Chucky 閱讀(454) 評論(0)  編輯  收藏 所屬分類: BBS Struts項目
          主站蜘蛛池模板: 仁化县| 锦州市| 望城县| 安岳县| 五莲县| 岢岚县| 平凉市| 卢湾区| 渭源县| 汾西县| 广宁县| 昆明市| 金塔县| 四平市| 宝应县| 濉溪县| 广东省| 铜山县| 宁河县| 博乐市| 德令哈市| 安图县| 哈尔滨市| 柳州市| 大洼县| 贡觉县| 嘉定区| 紫阳县| 荥阳市| 洛隆县| 莱阳市| 黑龙江省| 格尔木市| 洞口县| 察雅县| 外汇| 莆田市| 宜城市| 类乌齐县| 泗阳县| 齐齐哈尔市|