今天有人问前台表单form动态生成,后台使用DynaActionForm怎么用,说了他没懂,就写了个例子。
Action配置
<action
attribute="testMappingForm"
input="/jsp"
name="testMappingForm"
path="/testMapping"
scope="request"
type="com.modo.struts.action.TestMappingAction" />
ActionForm配置
<form-bean name="testMappingForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="fnames" type="java.util.HashMap"></form-property>
</form-bean>
请注意name属性,这个属性后面要用到。
然后是页面index.jsp,这里只模拟动态
<html:form action="/testMapping.do" method="post">
<%
for(int i=0;i<5;i++){
%>
<html:text property="<%="fnames(name_"+i+")"%>" value="<%="gangye_"+i%>"></html:text><br>
<%}%>
<br>
<html:submit value="Submit Form" />
</html:form>
请注意html:text的property标签。
index.jsp效果如下
后台Action
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
HashMap hm=(HashMap)((DynaActionForm)form).get("fnames");
Iterator it = hm.entrySet().iterator();
Map.Entry entry = null;
while(it.hasNext()){
entry = (Map.Entry)it.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
return null;
}
输出表单项
name_4 = gangye_4
name_0 = gangye_0
name_2 = gangye_2
name_1 = gangye_1
name_3 = gangye_3
posted on 2010-04-20 15:56
.Sun 阅读(2271)
评论(5) 编辑 收藏 所属分类:
随笔