動態表單和定製化Action

1. DynaActionForm
(1)爲什麼用動態表單
如果用ActionForm,每個頁面的表單都要寫一個ActionForm,每當表單修改時ActionForm都要修改和重新編譯
DynaActionForm直接在配置文件中配置,不用編碼,便於修改

(2)使用DynaActionForm的方法:
* 在struts-config.xml的<form-beans>標籤中添加:

<form-bean name="DynaForm" type="org.apache.struts.action.DynaActionForm" dynamic="true">
<form-property name=
"userName" type="java.lang.String" />
</form-bean>

* <action>的 name="DynaForm"
* 在Action中如果要訪問DynaActionForm
String userName = ( String )( ( DynaActionForm )form ).get(
"userName" );

因爲DynaActionForm是用map實現

2. 定製化Action -- DispatchAction

(1)應用場合:不同頁面,相同表單,執行不同的方法
(2)使用方法:
* 寫一個類繼承DispatchAction, 爲每個需要的服務寫一個方法
* 不用重寫 execute(), 因爲DispatchAction提供execute()
(3)例子:
* 在resource file 中添加:

account=account no.
amt=amount

submit.deposit=deposit
submit.withdraw=withdraw

* 寫兩個jsp頁面

//dispatch_dp.jsp
<%@ page contentType=
"text/html;charset=UTF-8" %>
<%@ taglib uri=
"/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri=
"/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri=
"/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:errors/>

<html:form action=
"/dpwd.do?method=deposit">
<bean:message key=
"account"/>
<html:text property=
"account" size="16" maxlength="16"/><br>

<bean:message key=
"amt" />
<html:text property=
"amount" size="16" maxlength="16"/><br>

<html:submit property=
"submit" value="deposit"/>
</html:form>

//dispatch_wd.jsp
<%@ page contentType=
"text/html;charset=UTF-8" %>
<%@ taglib uri=
"/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri=
"/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri=
"/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:errors/>

<html:form action=
"/dpwd.do?method=withdraw">
<bean:message key=
"account"/>
<html:text property=
"account" size="16" maxlength="16"/><br>

<bean:message key=
"amt" />
<html:text property=
"amount" size="16" maxlength="16"/><br>

<html:submit property=
"submit" value="withdraw"/>
</html:form>

* 在struts-config.xml配置<form-bean>
<form-bean name=
"DorWForm" type="org.apache.struts.validator.DynaValidatorForm" dynamic="true">
<form-property name=
"account" type="java.lang.String" />
<form-property name=
"amount" type="java.lang.String" />
</form-bean>

* 寫Action

//AccountAction
package hello;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*;
import org.apache.struts.util.*;

public final class AccountAction extends DispatchAction{
public ActionForward deposit( ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response ) throws Exception {

request.setAttribute(
"method", "deposit" );
return mapping.findForward(
"accountok" );
}

public ActionForward withdraw( ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response ) throws Exception {

request.setAttribute(
"method", "withdraw" );
return mapping.findForward(
"accountok" );
}
}

* 在struts-config.xml配置<action>
<action path =
"/dpwd"
type =
"hello.AccountAction"
name =
"DorWForm"
scope =
"request"
validate =
"false"
input =
"/dispatch_dp.jsp"
parameter=
"method"
>
<forward name=
"accountok" path="/accountok.jsp" />
</action>

* 寫accountok.jsp
method :<%= request.getAttribute(
"method" ) %>
<br>
<h1>ok</h1>

3. 定製化Action -- LookupDispatchAction

(1)應用場合:同一個表單,多個同名submit
(2)使用方法:
* 寫一個類繼承LookupDispatchAction, 爲每個需要的服務寫一個方法
* 重寫getKeyMethodMap(), map key resource file 中定義的key, map value 是方法名
LookupDispatchAction 是根據submit value去resource file 中找到key, 然後到 map 中找到方法
(3)例子:
* 寫dorw.jsp
<%@ page contentType=
"text/html;charset=UTF-8" %>
<%@ taglib uri=
"/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri=
"/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri=
"/WEB-INF/struts-logic.tld" prefix="logic" %>

<html:html locale=
"true">
<head>
<title>
<bean:message key=
"title"/>
</title>
<html:base/>
</head>

<body>
<html:errors/>

<html:form action=
"/dow.do" focus="userName">
<bean:message key=
"account"/>
<html:text property=
"account" size="16" maxlength="16"/><br>
<bean:message key=
"amt" />
<html:text property=
"amount" size="16" maxlength="16"/><br>
<html:submit property=
"action"><bean:message key="submit.deposit"/></html:submit>
<html:submit property=
"action"><bean:message key="submit.withdraw"/></html:submit>
</html:form>
</body>
</html:html>

* 寫Action
//DepositOrWithdrawAction
package hello;

import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.*;
import org.apache.struts.util.*;
import java.util.*;
import java.io.*;

public final class DepositOrWithdrawAction extends
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章