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

Web框架
Web框架:addOptions and removeAllOptions
Web框架:Xfire与Spring集成那些事
Web框架:多个dwr.xml配置方法
Web框架:小编整理Hibernate 基本查詢
Web框架:DWR使用中的web.xml配置
Web框架:Struts2使用Spring插件完成整合
Web框架:小编叙Spring的事务管理
Web框架:Struts2国际化实现用户自行选择语言
Web框架:Struts2中加载资源文件的方式
Web框架:Struts2中整合图表工具JFreeChart的时间顺序图
Web框架:浅谈Struts2的内建校验器
Web框架:FreeMarker中的escape , noescape指令
Struts2的Visitor校验器
Struts2中的subset标签使用方法浅谈
Hibernate核心接口那些事
Spring中的依赖注入
Spring中的Inversion of Control 容器
浅析Spring中的单元测试
用StrutsTestCase测试Struts应用程序
浅谈Struts中html:options的使用

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


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