(2)Collection和Map
简单List转换
//JSP代码
<%--
Created by IntelliJ IDEA.
User: mazhao
Date: 2007/09/04
Time: 12:47:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Point Page</title></head>
<body>
<s:form action="point" namespace="/point" method="post">
<s:textfield label="Point" name="point"/>
<s:textfield label="References" name="references"/>
<s:textfield label="References" name="references"/>
<s:textfield label="References" name="references"/>
<s:textfield label="References" name="references"/>
<s:textfield label="References" name="references"/>
<s:property value="point"/>
<s:submit/>
</s:form>
</body>
</html>
//Action代码
package com.jpleasure.action;
import com.jpleasure.conversion.Point;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
/**
* Created by IntelliJ IDEA.
* User: [email protected]
* Date: 2007/09/04
* Time: 12:45:11
* To change this template use File | Settings | File Templates.
*/
public class PointAction extends ActionSupport {
private Point point;
private List references;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public List getReferences() {
return references;
}
public void setReferences(List references) {
this.references = references;
}
public String execute() {
if(references == null) {
System.out.println("references is null");
} else {
System.out.println("references length is:" + references.size());
for(Object s: references) {
System.out.println("" + s);
}
}
return SUCCESS;
}
}
对象类型List转换(key-value pair 方式)
// Person 类型
package org.apache.struts2.showcase.conversion;
import java.io.Serializable;
/**
*
*/
public class Person implements Serializable {
private String name;
private Integer age;
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
public void setAge(Integer age) { this.age = age; }
public Integer getAge() { return this.age; }
}
// PersionAction 类型
package org.apache.struts2.showcase.conversion;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
/**
*
*/
public class PersonAction extends ActionSupport {
private List persons;
public List getPersons() { return persons; }
public void setPersons(List persons) { this.persons = persons; }
public String input() throws Exception {
return SUCCESS;
}
public String submit() throws Exception {
return SUCCESS;
}
}
// PersonAction转化配置文件PersonAction-conversion.properties
# PersonAction中persons属性(List类型)中元素的类型
Element_persons=org.apache.struts2.showcase.conversion.Person
// JSP部分代码
<s:iterator value="new int[3]" status="stat">
<s:textfield label="%{'Person '+#stat.index+' Name'}"
name="%{'persons['+#stat.index+'].name'}" />
<s:textfield label="%{'Person '+#stat.index+' Age'}"
name="%{'persons['+#stat.index+'].age'}" />
</s:iterator>
其中stat记录了当前循环的信息,其中stat.index表示当前循环的下标。
所以上述代码会生成如下的代码:
<s:textfield label="Person 1 Name"
name="persons[0].name" />
<s:textfield label="Person 1 Age"
name="persons[0].age" />
<s:textfield label="Person 2 Name"
name="persons[1].name" />
<s:textfield label="Person 2 Age"
name="persons[1].age" />
<s:textfield label="Person 3 Name"
name="persons[2].name" />
<s:textfield label="Person 3 Age"
name="persons[2].age" />
对象类型List转换(value 方式)
// Address 类型
package org.apache.struts2.showcase.conversion;
/**
* @version $Date: 2006-11-23 12:31:52 -0500 (Thu, 23 Nov 2006) $ $Id: Address.java 478625 2006-11-23 17:31:52Z wsmoak $
*/
public class Address {
private String id;
private String address;
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
}
// AddressAction 类型
package org.apache.struts2.showcase.conversion;
import java.util.LinkedHashSet;
import java.util.Set;
import com.opensymphony.xwork2.ActionSupport;
/**
* @version $Date: 2006-11-23 12:31:52 -0500 (Thu, 23 Nov 2006) $ $Id: AddressAction.java 478625 2006-11-23 17:31:52Z wsmoak $
*/
public class AddressAction extends ActionSupport {
private Set addresses = new LinkedHashSet();
public Set getAddresses() { return addresses; }
public void setAddresses(Set addresses) { this.addresses = addresses; }
public String input() throws Exception {
return SUCCESS;
}
public String submit() throws Exception {
System.out.println(addresses);
return SUCCESS;
}
}
//AddressAction转换配置文件AddressAction-conversion.properties
KeyProperty_addresses=id
Element_addresses=org.apache.struts2.showcase.conversion.Address
CreateIfNull_addresses=true
// JSP代码
<s:form action="submitAddressesInfo" namespace="/conversion">
<s:iterator value="%{new int[3]}" status="stat">
<s:textfield label="%{'Address '+#stat.index}"
name="%{'addresses(//'id'+#stat.index+'//').address'}" />
</s:iterator>
<s:submit />
</s:form>
上述代码会转换为:
<s:form action="submitAddressInfo" namespace="/conversion">
<s:textfield label="Address 0"
name="addresses('id0')" />
<s:textfield label="Address 1"
name="addresses('id1')" />
<s:textfield label="Address 2"
name="addresses('id2')" />
<s:submit />
</s:form>
注意两个地方:
第一,没有向服务器提交Address的id属性,那么Address的id属性是什么呢?
KeyProperty_addresses=id表示向服务器提交的内容的key部分("id0”, "id1”, "id2”)会被认定为Addredd的id。
第二,CreateIfNull_addresses=true表示及时客户端没有向服务器提交任何Address内容,服务器也会为AddressAction的addresses 建立一个长度为0的Set。
开发的过程中不要一味的使用String类型,使用String类型无论在处理的速度还是内存的使用上都不是最好的方式。一般情况下,Java的模型类(JavaBean, Action等都可以视为Java模型类,因为其中表示了模型的信息),一般情况下需要和数据库中的类型一致。这样才能保证最好的性能。
那么像java.util.Date,Integer等类型需要表示到JSP页面上的时候还是需要表示为String类型的,但是Struts2都已经帮助实现了,所以请使用具体的类型吧,不要总是使用String类型。