当前位置: 首页 > 图文教程 > Java技术 > Web框架 > Web框架:利用列表数据提高开发效率

Web框架
浅谈Ajax与Spring集成
浅谈Mock和基于Spring容器管理事务的测试
Hibernate中一级缓存与二级缓存
浅谈Hibernate的配置信息(一)
浅谈Hibernate的配置信息(二)
浅谈Spring中使用Quartz来执行计划任务
浅谈JFreeChart实时曲线(代码)
Ajax框架中选DWR还是A4J
浅谈Hibernate中关联问题
浅谈项目开发过程中使用Struts-menu
浅谈使用模板模式简化操作Hibernate
Hibernate中加载并存储对象
浅谈Hibernate依赖对象(Dependent objects)
浅谈Hibernate中自定义值类型
ORM那些事
51培训对Hibernate中的事务详解
51培训详谈Hibernate检索
详谈Struts2的核心概念(上)
详谈Struts2的核心概念(下)
浅谈应用dom4j操作xml文件

Web框架:利用列表数据提高开发效率


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 115 ::
收藏到网摘: n/a

效率对一个开发团队来讲至关重要,Struts2本身提供了很多提高效率的方法,只要你有双善于发现的眼睛!Struts2可以通过list来把列表数据在HTML用户界面和action之间进行便捷的传递。让我们看一个例子。这是一个Person类,每一个属性都有对应的gettersetter(这里省略掉了)::

public class Person {

int id;

String name;

int age;

float height;

}

我们的action需要有一个person列表:

public class MyAction {

public List getPeopleList() { … }

public void setPeopleList( List peopleList ) { … }

}

在把Person类作为MyAction中的一个元素使用之前,我们需要添加一些配置信息。在MyAction的同一个package下面创建一个名为“MyAction-conversion.properties”的文件。该文件名和验证文件的命名方式一样,在action名字后面跟着“*-conversion.properties”后缀。文件内容为:

Element_peopleList=Person

Element_”前缀是一个常量,它后面的“peopleList”是action类中的list属性名。等式右边是要放到list里面的类的全限定名。

最后,我们还需要把这个list渲染给用户:

<ww:iterator value="peopleList" status="stat">

<s:property value="peopleList[#stat.index].id" />

<s:property value="peopleList[#stat.index].name" />

<s:property value="peopleList[#stat.index].age" />

<s:property value="peopleList[#stat.index].height" />

</ww:iterator>

因为list是有索引的,所以我们使用了迭代器状态对象的索引属性来引用被显示的对象。对当前的例子而言,这不是最有效率的办法;因为<s:property />标签的value还可以简化成“id”,“name”,“age”和“height”。这里只是想让你明白,要得到可编辑的表单需要哪些东西。

<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>

注意这段代码中的“name”和“value”属性和上面的“value”属性很相似。区别在于,在“name”属性中,我们需要用正确的token把“#stat.index”包起来,从而得到实际的索引值,再用它来获取真正的值。而在“value”中,整个表达式都被包了起来。Struts2用这段代码生成一个ArrayList,里面都是计算后的People对象,然后用setPeopleList()方法把这个ArrayList赋值给action

为了允许Struts2可以在这个list中创建新的对象(可能你是在用户界面中动态创建对象的),把下面这行代码加入“MyAction-conversion.properties”文件中:

CreateIfNull_peopleList = true