云自无心水自闲

天平山上白云泉,云自无心水自闲。何必奔冲山下去,更添波浪向人间!
posts - 288, comments - 524, trackbacks - 0, articles - 6
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Struts2中实现可编辑的表格

Posted on 2008-02-12 21:06 云自无心水自闲 阅读(3224) 评论(2)  编辑  收藏 所属分类: Java心得体会Struts2
Struts2中支持使用List在页面和Action之间直接传递表格数据。下面是一个示例:
public class Person {
int id;
String name;
int age;
float height;
}

这是一个POJO,getter和setting省略了。

action中可以这样使用:

public class MyAction {
public List getPeopleList() { … }
public void setPeopleList( List peopleList ) { … }

}
在我们使用Person类之前,需要添加一个配置文件,MyAction-conversion.properties,把这个文件和MyAction放在一起。
这个文件里只有一行内容:
Element_peopleList=Person
前缀Element_是一个常量,表明等号左边的表达式中跟在这个常量后面的是Action类中一个List类型的字段名。
等号右边的表达式是全类名(包含package)
下面是一个页面的代码片段:
<s:form action="update" method="post" >
<s:iterator value="peopleList" status="stat">
<s:hidden
name="peopleList[%{#stat.index}].id"
value
="%{peopleList[#stat.index].id}"/>
<s:textfield label="Name"
name
="peopleList[%{#stat.index}].name"
value
="%{peopleList[#stat.index].name}"/>
<s:textfield label="Age"
name
="peopleList[%{#stat.index}].age"
value
="%{peopleList[#stat.index].age}" />
<s:textfield label="Height"
name
="peopleList[%{#stat.index}].height"
value
="%{peopleList[#stat.index].height}"/>
<br/>
</s:iterator>
<s:submit value="Update"/>
</s:form>


使用这段代码,Struts2会创建一个Person类的ArrayList,并且用setPersonList这个方法把页面表格中的值传递回Action。
如果你是想从用户界面中动态创建列表值,需要允许Struts2给列表中类的实例。那么在配置文件MyAction-conversion.properties中添加一行:
CreateIfNull_peopleList = true


评论

# re: Struts2中实现可编辑的表格  回复  更多评论   

2009-04-24 19:51 by 路人甲
我尝试用你的这种方式处理,可是不行啊。。??

# re: Struts2中实现可编辑的表格  回复  更多评论   

2009-04-24 22:28 by usherlight
@路人甲
是什么问题呢?能说明的详细一些吗?

只有注册用户登录后才能发表评论。


网站导航: