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

Web框架
struts2(三)
struts2(四)
struts2(五)
struts2(六)
Spring中注入概念的简单介绍
深入探讨struts的ActionMessages消息机制(转)
DWR 的 Converter 实现原理简单分析及应用
详解Spring中bean的作用域
AJAX实现级联下拉框
Spring AOP面向方面编程原理:AOP概念
Spring结合Hibernate声明式事务配置
Struts 概述
Ajax 入门简介
Struts配置文件详解
Struts in Spring
Struts 开发的最佳实践
Struts核心标签
开发框架:利用STRUTS实现国际化支持
Struts自定义标签的过程
Struts 框架的基本概念及实现MVC模式的原理

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 116 ::
收藏到网摘: 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