第七章 Type Convertion
为什么会有类型转换?
point = com.jpleasure.convertor.PointConverter
也就是说画面一个叫做point的项目(input类型,name为point)提交到服务器上之后,在向Action中的point属性赋值之前需要使用PointConverter将字符串转换为Point类,在Action中的point属性向画面显示的时候需要使用PointConverter将Point类转换为字符串类型。
我们使用一个例子来展现如何实现TypeConvertor类型:
// Point 类
package com.jpleasure.conversion;
/**
* Created by IntelliJ IDEA.
* User: ma.zhao@dl.cn
* Date: 2007/09/04
* Time: 12:33:43
* To change this template use File | Settings | File Templates.
*/
public class Point {
private int x;
private int y;
public Point() {
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String toString() {
StringBuffer sb = new StringBuffer("Point(");
sb.append(x).append(", ").append(y).append(")");
return sb.toString();
}
}
// PointConvertor 类
package com.jpleasure.conversion;
import org.apache.struts2.util.StrutsTypeConverter;
import java.util.Map;
/**
* Created by IntelliJ IDEA.
* User: ma.zhao@dl.cn
* Date: 2007/09/04
* Time: 12:34:18
* To change this template use File | Settings | File Templates.
*/
public class PointConvertor extends StrutsTypeConverter {
// 从字符串转换为对象的方法。
public Object convertFromString(Map map, String[] strings, Class aClass) {
if (strings.length > 0) {
String pointStr = strings[0];
String[] pointStrArray = pointStr.split(",");
if (pointStrArray.length == 2) {
Point p = new Point();
p.setX(Integer.parseInt(pointStrArray[0]));
p.setY(Integer.parseInt(pointStrArray[1]));
return p;
} else {
return null;
}
} else {
return null;
}
}
// 从对象转换为字符串的方法。
public String convertToString(Map map, Object o) {
if (o instanceof Point) {
return o.toString();
} else {
return "";
}
}
}
// 测试用PointAction类
package com.jpleasure.action;
import com.jpleasure.conversion.Point;
import com.opensymphony.xwork2.ActionSupport;
/**
* Created by IntelliJ IDEA.
* User: ma.zhao@dl.cn
* 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;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public String execute() {
return SUCCESS;
}
}
// JSP文件
<%--
Created by IntelliJ IDEA.
User: ma.zhao@dl.cn
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 name="point"/>
<s:property value="point"/>
<s:submit/>
</s:form>
</body>
</html>
// PointAction配置文件类。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="point" namespace="/point" extends="struts-default">
<action name="point" class="com.jpleasure.action.PointAction">
<result>/point/Point.jsp</result>
<!-- 定义input的result是为了处理转换出错的情况,在输入格式不正确的情况下转移到这个画面 -->
<result name="input">/point/Point.jsp</result>
</action>
<!-- Add actions here -->
</package>
</struts>
(1)处理Null值
有些时候我们会被NullPointerException搞的焦头烂额,为什么系统不能为我们定义了但是没有初始化的对象建立一个空的Object的引用呢?Struts2有这个功能,但是在默认情况下Struts2关闭了这个功能,要想开启这个功能,需要在ParameterInterceptor开始处理参数之前在ValueStack中将一个值开启,这个值是: InstantiatingNullHandler.CREATE_NULL_OBJECTS。
在Java代码中 InstantiatingNullHandler.CREATE_NULL_OBJECTS的值是:xwork.NullHandler.createNullObjects
创建空值对象的规则为:
- 如果属性声明为Collection或List, 将返回一个ArrayList并赋值给空引用.
- 如果属性声明为Map, 将返回一个HashMap并赋值给空引用.
- 如果空值属性是一个带有无参构造函数的简单Bean, 将使用ObjectFactory.buildBean(java.lang.Class, java.util.Map)方法创建一个实例.
(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: ma.zhao@dl.cn
* 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类型。