struts2 类型转换 首先当然要配置好struts2的环境 。
第一步先先写一个登入界面
input.jsp
<body> <h1>input a point coordinate(divide with ",")</h1> <form action="converterAction.action" method="post">
coordinate:<input type="text" name="point" size="20"/><br> <!-- 需要类型转换 --> username:<input type="text" name="username" size="20"/><br> age:<input type="text" name="age" size="20"/><br> birthday:<input type="text" name="birthday" size="20"/><br> <input type="submit" value="submit"/> <input type="reset" value="reset"/> </form> </body>
根据表单 写Action
<action name="converterAction" class="test.struts2.PointAction">
<result name="success">/output.jsp</result>
这个是Action类 PointAction.java
package test.struts2;
import java.util.Date;
import test.bean.Point;
import com.opensymphony.xwork2.ActionSupport;
public class PointAction extends ActionSupport
{
/**
*
*/
private static final long serialVersionUID = 1L;
private Point point;
private String username;
private int age;
private Date birthday;
public Point getPoint()
{
return point;
}
public void setPoint(Point point)
{
this.point = point;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
@Override
public String execute() throws Exception
{
return SUCCESS;
}
}
struts2 对于基本数据类型会自动转换 对于Point 我们就要自己转换
定义一个properties 文件 文件名有严格的规定 Action类-conversion.properties
如: PointAction-conversion.properties
里面的代码就是要转换的类型与转换类
point=test.converter.PointConverter
(point就是要转换的类)
其次就是编写PointConverter类 (这个必须继承DefaultTypeConverter 类)
package test.converter;
import java.util.Map;
import ognl.DefaultTypeConverter;
import test.bean.Point;
public class PointConverter extends DefaultTypeConverter
{
/*
* tyoType 表示要转换的类型是什么 (是对象转换成字符 还是字符转换成对象)
*/
@SuppressWarnings("unchecked")
@Override
public Object convertValue(Map context, Object value, Class toType)
{
if(Point.class == toType) //要转换成point
{
System.out.println("toType = Point");
String[] str = (String[])value;
String firstValue = str[0]; // 取得传进来的坐标字符串
String[] resultValue = firstValue.split(",");
Point point = new Point();
point.setX(Integer.valueOf(resultValue[0]));
point.setY(Integer.valueOf(resultValue[1]));
return point;
}
else if(String.class == toType)
{
System.out.println("toType = String");
Point point = (Point)value;
int x = point.getX();
int y = point.getY();
String result = "x:" + x + "y:" + y;
return result;
}
return null;
}
}
到这就基本完成了 写个输出文件 out.jsp
<body>
coordinate: <s:property value="point"/><br>
username: <s:property value="username"/><br>
age: <s:property value="age"/><br>
birthday:<s:property value="birthday"/>
</body>
(用了struts2的标签库)
这个是Point类
package test.bean;
public class Point
{
private int x;
private int 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;
}
}
完成!
其中要注意的是提交表单那些name 必须与Action类中的set get name相同不然会出 ognl.NoConversionPossible
错误 ! 我因为将提交表单的point 写错了一个字母 导致出现ognl.NoConversionPossible错误 , 害我找了好久,印象特别深!