我是靠谱客的博主 迷你招牌,最近开发中收集的这篇文章主要介绍动态表单和定制化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

最后

以上就是迷你招牌为你收集整理的动态表单和定制化Action 的全部内容,希望文章能够帮你解决动态表单和定制化Action 所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部