Struts之DispatchAction使用(錄像教程)

          Posted on 2005-12-04 10:46 oksonic 閱讀(8381) 評論(10)  編輯  收藏 所屬分類: java

          速動畫教程第十三集

           

          下載地址:http://sonic.peakle.net/download/sonic013.rar

           

          Struts 之 DispatchAction

           

          介紹

              DispatchAction就是在struts-config中用parameter參數配置一個表單字段名,這個字段的值就是最終替代execute被調用的方法。

              例如parameter="method"而request.getParameter("method")="save",其中"save"就是MethodName。struts的請求將根據parameter被分發到"save"或者"edit"或者什么。但是有一點,save()或者edit()等方法的聲明和execute必須一模一樣。

           

          新建工程:test

          添加Struts框架

           

          創建index.jsp

           

          按下Ctrl + N ,創建add.jspUsersAction.java

          ActionForm采用動態的ActionForm,所以繼承于DynaActionForm

          UserAction的內容將包含add、delall等方法,并且繼承于DispatchAction

           

          * 記得修改AddAction.java 為 UsersAction

           

          <action

                attribute="addForm"

                input="/add.jsp"

                name="addForm"

                parameter="method"

                path="/add"

                scope="request"

                 validate="false"

                type="com.test.struts.action.UsersAction" />

           

          * 綠色字全部份為參數

           

          新建一個forward,名稱為indexGo,并指向index.jsp,Relative設置為true

           

          修改add.jsp文件

                        <html:form action="/add">

                     username : <html:text property="username"/><html:errors property="username"/><br/>

                     <html:submit onclick="document.forms[0].action='add.do?method=add';document.forms[0].submit();"/><html:cancel/>

                 </html:form>

           

          * 綠色字為修改部份

          修改后的提交方式是帶參數提交的,不過必須點提交按鈕,如果是使用回車鍵的話就不會帶有參數

           

          修改UsersAction.java文件

          增加以下代碼:

              public ActionForward add(ActionMapping mapping, ActionForm form,

                      HttpServletRequest request, HttpServletResponse response) {

                  DynaActionForm addForm = (DynaActionForm) form;

                  String username = addForm.getString("username");

                  // 驗證用戶輸入

                  if (username == null || username.length() < 1)

                      mapping.getInputForward();

                  HttpSession session = request.getSession();

                  // 從session中獲得數據

                  Vector users = (Vector) session.getAttribute("Users");

                  if (users == null)

                      users = new Vector();

                  users.addElement(username);

                  session.setAttribute("Users", users);

                  return mapping.findForward("indexGo");

              }

           

           

          修改index.jsp文件,使頁面中可以顯示session中的數據,代碼如下:

          <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

          <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

          <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

          <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

          <html>

            <head>

              <title>INDEX</title>

            </head>

           

            <body>

              <a href="add.jsp">ADD USER</a><br>

              <a href="delete.jsp">DELETE ALL</a><p>

              <logic:present name="Users">

              <logic:iterate id="element" name="Users">

                  <bean:write name="element"/><br>

              </logic:iterate>

              </logic:present>

            </body>

          </html>

           

           

          按下Ctrl + N ,創建DellallAction.java,繼承于DispatchAction

          選中:Use existing Action class,瀏覽UsersAction

          選中:Parameter選項卡,填入method,然后完成

           

          現在修改index.jsp文件

          <a href="delete.jsp">DELETE ALL</a><p>

          改為

          <a href="delall.do?method=delall">DELETE ALL</a><p>

           

          修改UsersAction.java文件

          增加以下代碼:

              public ActionForward delall(

                      ActionMapping mapping,

                      ActionForm form,

                      HttpServletRequest request,

                      HttpServletResponse response) {

                      HttpSession session=request.getSession();

                      session.setAttribute("Users",null);

                      return mapping.findForward("indexGo");

                  }

           

          這一步很重要,execute 方法必須刪除!!!

           

          好了,可以進行測試了!!!

          Feedback

          # re: 速動畫教程第十三集Struts之DispatchAction  回復  更多評論   

          2005-12-05 10:53 by 鐵手
          So cool so good. pls go on. Supporting!

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2006-01-08 18:58 by ooad
          感謝oksonic:
          你的例子我作了,很好,在此表示感謝!
           但是怎末解決回車不帶參數呢?
          還有就是我看到大部分都是繼承于LookUpDispatchAction 他們有什么區別呢??
          盼望回復.

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2006-03-15 15:51 by xs
          不知道為什么我做的例子每次加進去兩條 ,請幫我解答一下。謝謝

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2006-03-22 10:41 by achun
          感謝一下!

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2006-03-31 18:38 by 雨來
          好偉大啊

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2006-05-08 16:22 by lingdian
          樓主,為什么你的gmail里沒有這個的sonic013的文件啊!!!

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2006-07-13 10:27 by abc
          * 綠色字為修改部份

          修改后的提交方式是帶參數提交的,不過必須點提交按鈕,如果是使用回車鍵的話就不會帶有參數

          -----------------請問如何解決回車提交 ?

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2006-09-14 21:11 by hawks
          下面這段代碼
          <table align=center border="15" cellspacing="2" width="89%"> <br>
          <tr>
          <th>用戶名 </th><th>角色</th><th>刪除</th>
          </tr>
          <logic:iterate id="rs" name="queryResult">
          <tr>
          <form action="/searchAction.do?expression=<bean:write name='rs' property='username' />">
          <td><bean:write name="rs" property="username" /></td>
          <td><bean:write name="rs" property="role" /></td>
          <td>
          <html:submit value="remove" onclick="document.forms[0].action='searchAction.do?method=remove';document.form[0].submit();"/>
          </td>
          </form >
          </tr>
          </logic:iterate>
          </table>
          用form[0]不合適,需要一個變量,類似form[i],應該怎樣;或者是有其他的方法,例如bean:message。不知道怎么實現,請教樓主。

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2006-12-18 14:54 by LeVaN
          http://www.anna-sen-soida-elastinen.beibi.info ^^^ http://www.anssi-22-vuosi-joensuu.beibi.info ^^^ http://www.eldre-masturbering-mpeg.biseksuell.info ^^^ http://www.film-prostituert-hetest.biseksuell.info ^^^ http://www.eldre-masturbering-mpeg.erotiska.info ^^^ http://www.film-prostituert-hetest.erotiska.info ^^^ http://www.film-snall-homosexuell.fitta69.info ^^^ http://www.videor-transsexuell-rakning.fitta69.info ^^^ http://www.sympatisk-foto-mpg.fotsex.info ^^^ http://www.naturlig-penis-klipp.fotsex.info ^^^ http://www.naida-gratissex.isomuna.info ^^^ http://www.uhkarohkea-poliisi-striptease.isomuna.info ^^^ http://www.sopo-tytot-vittu.laukeaminen.info ^^^ http://www.pelottava-lesbo-pano.laukeaminen.info ^^^ http://www.porno-kukk-jenter.rype.info ^^^ http://www.klipp-munnsex-strippe.rype.info ^^^ http://www.knulle-pornostjerne-porno.sadsprut.info ^^^ http://www.eksotisk-bitch-bilde.sadsprut.info ^^^ http://www.onnelliset-orgasmi.tytsy.info ^^^ http://www.latino-pillut-imeskella.tytsy.info ^^^ http://www.masturbation-creampie-ass.18analsex.com ^^^ http://www.anl-fucking-xxx.18analsex.com ^^^

          # re: Struts之DispatchAction使用(錄像教程)  回復  更多評論   

          2008-01-18 13:32 by 插兩條記錄的解決方法
          <html:button value="submit" property="submit" onclick="document.forms[0].action='record.do?method=add';document.forms[0].submit();"/><html:cancel/>

          posts - 103, comments - 1104, trackbacks - 0, articles - 0

          Copyright © oksonic

          主站蜘蛛池模板: 湟源县| 大理市| 禄丰县| 乌兰察布市| 丰城市| 万安县| 嵊泗县| 天津市| 彭山县| 太原市| 钦州市| 思南县| 娱乐| 本溪市| 宜君县| 贞丰县| 蒲城县| 张家川| 兴义市| 湖州市| 富蕴县| 通江县| 景宁| 遵义县| 木兰县| 文化| 凯里市| 奎屯市| 昭平县| 寿光市| 青州市| 唐山市| 简阳市| 新野县| 西充县| 南投市| 利津县| 桐柏县| 进贤县| 澄城县| 平安县|