兩畝三分地

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            17 隨筆 :: 20 文章 :: 2 評論 :: 0 Trackbacks
          <2009年10月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          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項目
          主站蜘蛛池模板: 杭锦后旗| 和田市| 正宁县| 岳阳县| 义马市| 沽源县| 亳州市| 灵川县| 准格尔旗| 资阳市| 太原市| 榆树市| 襄城县| 天等县| 绵阳市| 梧州市| 凤阳县| 天台县| 南投县| 久治县| 旅游| 仲巴县| 林州市| 临猗县| 兴文县| 崇仁县| 英超| 海晏县| 高唐县| 五峰| 漠河县| 合江县| 大理市| 华安县| 荥阳市| 理塘县| 贵州省| 金堂县| 板桥市| 仁布县| 马关县|