天天看點

Struts DispatchAction

引自友人blog: [url]http://blog.csdn.net/senton[/url]

介紹

    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.jsp、usersaction.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>

* 綠色字為修改部份

修改後的送出方式是帶參數送出的,不過必須點送出按鈕,如果是使用Enter鍵的話就不會帶有參數

修改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>

    public actionforward delall(

            actionmapping mapping,

            actionform form,

            httpservletrequest request,

            httpservletresponse response) {

            httpsession session=request.getsession();

            session.setattribute("users",null);

            return mapping.findforward("indexgo");

        }

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

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

繼續閱讀