动态表单和定制化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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值