struts2动态表单

MyAction.java

public class MyAction extends AbstractAction {

 private List personList;
 
 // 生成表单
 public String execute() { 
  personList = buildPersonList();
  return this.SUCCESS;
 }
 
 // 处理提交的表单
 public String update(){
  Iterator it = personList.iterator();
  while(it.hasNext()){
   Person p = (Person)it.next();
   System.out.println("===============" p.getName());
  }
  return this.SUCCESS;
 }
 
 private List buildPersonList(){
  List list = new ArrayList();
  for(int i=0; i<5; i ){
   Person p = new Person();
   p.setId(i);
   p.setAge(20 i);
   p.setName("pengbing" i);
   list.add(p);
  }
  return list;
 }

 

Person.java


public class Person {
 private int id;
 private String name;
 private int age;

............

 

 

-----------------------------------------------
在MyAction下建MyAction-conversion.properties 这个文件,没有这个文件就完成不了该功能

# 指定personList里的对象
Element_personList=cn.upge.struts2study.editForm.Person

# 如果personList为空,允许创建personList
CreateIfNull_personList=true 

-----------------------------------------------

<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<s:form action="update.action">
 <s:iterator value="personList" status="stat">
  <s:textfield name="personList[%{#stat.index}].id" value="%{personList[#stat.index].id}" />
  <s:textfield name="personList[%{#stat.index}].name" value="%{personList[#stat.index].name}" />
  <s:textfield name="personList[%{#stat.index}].age" value="%{personList[#stat.index].age}" />
 </s:iterator>
 
 <s:submit />
</s:form>

 

 

来自:http://blog.sina.com.cn/s/blog_4c67de6f0100bbvb.html

你可能感兴趣的:(html,struts,Blog)