<h3><font color="red">使用x坐标,y坐标分两行</font></h3>
<s:form action="pointConverter">
<s:textfield name="point.x" label="x"></s:textfield>
<s:textfield name="point.y" label="y"></s:textfield>
<s:textfield name="age" label="age"></s:textfield>
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="date" label="birthday"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
一、当输入页面有多个Point类的对象时,point,point2,point3
那么相应的输入页面,结果页面,PointAction,PointAction-conversion.properties都改成三个。
point=com.test.converter.PointConverter
point2=com.test.converter.PointConverter
point3=com.test.converter.PointConverter
二、那么如果有十个呢?不是需要在配置文件中写十个,比较麻烦
有以下方法:全局的类型的类型转换
位置Src目录下,不在和相应的Action在一起了
名字叫xwork-conversion.properties
内容:com.test.bean.Point=com.test.converter.PointConverter
等号左边表示的需要转化的类名(含包名),右侧是转化类。
三、此时举例的转换类PointConverter2.java(基于Struts2的)更加简洁。一般正式项目中用的也是这个。
//继承StrutsTypeConverter并且重写convertFromString()和convertToString方法。
package com.test.converter;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.test.bean.Point;
public class PointConverter2 extends StrutsTypeConverter
{
@Override
public Object convertFromString(Map context, String[] values, Class toClass)
{
Point point = new Point();
String[] paramValues = values[0].split(",");
int x = Integer.parseInt(paramValues[0]);
int y = Integer.parseInt(paramValues[1]);
point.setX(x);
point.setY(y);
return point;
}
@Override
public String convertToString(Map context, Object o)
{
Point point = (Point)o;
int x = point.getX();
int y = point.getY();
String result = "[ x = " + x + " , y = " + y + " ]";
return result;
}
}
四、转换集合类型 List<Point>
1)即在PointAction.java类为
package com.test.action;
import java.util.Date;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.test.bean.Point;
public class PointAction extends ActionSupport
{
private List<Point> point;
private int age;
private String username;
private Date date;
public List<Point> getPoint()
{
return point;
}
public void setPoint(List<Point> point)
{
this.point = point;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public Date getDate()
{
return date;
}
public void setDate(Date date)
{
this.date = date;
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
2)此时的转化类可修改为PointConverter3.java
package com.test.converter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.test.bean.Point;
public class PointConverter3 extends StrutsTypeConverter
{
@Override
public Object convertFromString(Map context, String[] values, Class toClass)
{
List<Point> list = new ArrayList<Point>();
for(String value : values)
{
Point point = new Point();
String[] paramValues = value.split(",");
int x = Integer.parseInt(paramValues[0]);
int y = Integer.parseInt(paramValues[1]);
point.setX(x);
point.setY(y);
list.add(point);
}
return list;
}
@Override
public String convertToString(Map context, Object o)
{
List<Point> list = (List<Point>)o;
StringBuilder sb = new StringBuilder();
sb.append("[ ");
int number = 0 ;
for(Point point : list)
{
++number;
int x = point.getX();
int y = point.getY();
sb.append(number).append(" x = ").append(x).append(" , y = ").append(y).append(" ");
}
sb.append("]");
return sb.toString();
}
}
3)input.jsp
<s:form action="pointConverter">
<s:textfield name="point" label="point"></s:textfield>
<s:textfield name="point" label="point2"></s:textfield>
<s:textfield name="point" label="point3"></s:textfield>
<s:textfield name="age" label="age"></s:textfield>
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="date" label="birthday"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
4)output.jsp
point:<s:property value="point"/><br>
age:<s:property value="age"/><br>
username:<s:property value="username"/><br>
date:<s:property value="date"/><br>
五、将x坐标,y坐标分两行输入页面
1)input.jsp
2)此时就不用任何类型转化了,就可以自动准确输出了
3)采用这种方式“.”对象.属性的方式的话,Point必须提供一个不带参数的构造方法
其实strut2内部采用了反射的机制!不建意用这个方式,表单太罗嗦了。