struts2学习笔记四(第4讲.Struts2的类型转换续)
struts2中的局部类型转换
新的需求:用户需要同时增加三个点的坐标。
一、找到之前创建的input.jsp页面,再增加两个textfield,用于输入新增的两个点的坐标:
<body><h3><font color="red">使用逗号将点的两个坐标分隔开</font></h3><s:form action="pointConverter"><s:textfield name="point" label="point"></s:textfield><s:textfield name="point2" label="point2"></s:textfield><s:textfield name="point3" 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> </body>
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 Point point2;private Point point3;private int age;private String username;private Date date;public Point getPoint() {return point;}public void setPoint(Point point) {this.point = point;}public Point getPoint2() {return point2;}public void setPoint2(Point point2) {this.point2 = point2;}public Point getPoint3() {return point3;}public void setPoint3(Point point3) {this.point3 = point3;}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;}@Overridepublic String execute() throws Exception {return SUCCESS;}}point=com.test.converter.PointConverterpoint2=com.test.converter.PointConverterpoint3=com.test.converter.PointConverter
<body> point:<s:property value="point"/><br> point2:<s:property value="point2"/><br> point3:<s:property value="point3"/><br> age:<s:property value="age"/><br> username:<s:property value="username"/><br> date:<s:property value="date"/> </body>



com.test.bean.Point=com.test.converter.PointConverter
#point=com.test.converter.PointConverter#point2=com.test.converter.PointConverter#point3=com.test.converter.PointConverter
package com.test.converter;import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter;import com.test.bean.Point;public class PointConverter2 extends StrutsTypeConverter {@Overridepublic 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;}@Overridepublic 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;}}com.test.bean.Point=com.test.converter.PointConverter2
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 Point point;//private Point point2;//private Point point3;private List<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 Point getPoint2() //{//return point2;//}////public void setPoint2(Point point2) //{//this.point2 = point2;//}////public Point getPoint3() //{//return point3;//}////public void setPoint3(Point point3) {//this.point3 = point3;//}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;}@Overridepublic String execute() throws Exception {return SUCCESS;}}<body><h3><font color="red">使用逗号将点的两个坐标分隔开</font></h3><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> </body>
package com.test.converter;import java.util.*;import org.apache.struts2.util.StrutsTypeConverter;import com.test.bean.Point;public class PointConverter3 extends StrutsTypeConverter {@Overridepublic 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;}@Overridepublic 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();}}#point=com.test.converter.PointConverter#point2=com.test.converter.PointConverter#point3=com.test.converter.PointConverterpoint=com.test.converter.PointConverter3
#com.test.bean.Point=com.test.converter.PointConverter3
<body> point:<s:property value="point"/><br> age:<s:property value="age"/><br> username:<s:property value="username"/><br> date:<s:property value="date"/> </body>
