struts2学习之三、类型转换
1、本例中在界面上输入点,所以引入了类型转换
点的pojo类
package com.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; } @Override public String toString() { return "x = " + x + " y = " + y; } }
2、input.jsp
... <body> <h3><font color="red">使用逗号将点的两个坐标分割开</font></h3> <s:form action="pointConverter"> <s:textfield name="point" label="point"></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> </body> ...
3、ognl,使用struts时最好去参考一下,webwork网站上的ognl--》source repository--》通过wincvs客户端可以将源代码下载下来
4、转换类PointConverter.class(基于webwork的ognl)
package com.test.converter;
import java.util.Map;
import ognl.DefaultTypeConverter;
import com.test.bean.Point;
//此处继承了strut2中的ongl的jar包中DefaultTypeConverter类,该类有两个转换类,我们重写它即可!
public class PointConverter extends DefaultTypeConverter { //context上下文 @Override public Object convertValue(Map context, Object value, Class toType) { //toType表示转换成什么,此处表示要从字符串转换到point if(Point.class == toType) { Point point = new Point(); //解释:为什么此处是字符串数组,因为提交过来可能是一个组,比如一个选择列表,他们的名字都是相同的。 String[] str = (String[])value; String[] paramValues = str[0].split(","); int x = Integer.parseInt(paramValues[0]); int y = Integer.parseInt(paramValues[1]); point.setX(x); point.setY(y); return point; } if(String.class == toType) { Point point = (Point)value; int x = point.getX(); int y = point.getY(); String result = "[x=" + x + " , y=" + y + "]"; return result; } return null; } }
5、pointAction类
package com.test.action; import java.util.Date; import com.opensymphony.xwork2.ActionSupport; import com.test.bean.Point; public class PointAction extends ActionSupport { private Point point; private int age; private String username; private Date date; public Point getPoint() { return point; } public void setPoint(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; } }
6、out.jsp
<body> point:<s:property value="point"/><br> age:<s:property value="age"/><br> username:<s:property value="username"/><br> date:<s:property value="date"/><br> </body>
7、struts.xml
<action name="login" class="com.test.action.LoginAction"> <result name="input">/login2.jsp</result> <result name="success">result.jsp</result> <result name="failer">/login2.jsp</result> <action name="pointConverter" class="com.test.action.PointAction"> <result name="success">/output.jsp</result> </action>
struts如何知道去找谁转换呢?
1)规定在src定义一个文件(系统会自动拷贝一份到class目录下一份)
文件名为PointAction-conversion.proreties
该文件名字要求,文件名格式为***-conversion.properties
文件位置和Action类放在一起,且名前半部分和Action类一致如:PointAction-conversion.proreties
内容为point=con.test.converter.pointConverter
解释:等号左侧是需要转换的类的对象,左侧是用哪个类来转换。
2)运行过程当运行到PointAction类时,发现有一个文件PointAction-conversion.proreties,那么它在调用action里面的属性时它就会
检查需不需要转化,当执行到setPoint的时候,发现了Point类需要转化,会先对Point类根据=con.test.converter.pointConverter来转化。然后再赋值。
当需要跳转到success对应页面输出时,发现有Point输出时,又一次类型转化!