1,修改上一节的input.jsp文件
<s:form action="pointConvert"> <s:textfield name="point1" label="point1"></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></s:submit> </s:form>
新建了point2,point3两个变量,如果照上一节的方法进行类型转换,需要在PointAction-conversion.properties文件中新建两个配置,即
point2=com.test.convert.PointAction point3=com.test.convert.PointAction
如果有多个这样的变更,这样做就太繁琐。
2,使用xwork的方式进行类型转换,在src目录下新建一个文件:xwork-conversion.properties
#所有Point类型的实例都用PointConverter进行转换 com.test.bean.Point=com.test.converter.PointConverter2
这样就第1步中的方式简单很多,可以根据类型进行转换,而不需对每个变量都进行一次配置。
同时PointConvert.java类也要进行改造,需继承StrutsTypeConverter
package com.test.convert; import java.util.Map; import org.apache.struts2.util.StrutsTypeConverter; import com.test.bean.Point; public class PointConvert extends StrutsTypeConverter { 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; } 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; } }
struts.xml文件还是上一节的配置,没有修改。
output.jsp文件修改为
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> point1:<s:property value="point1"/><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"/><br> </body> </html>
3,如果要进行LIST类型转换,应该怎么做?先对input.jsp进行改造。有三个name为point的输入框。
<s:form action="pointConvert"> <s:textfield name="point" label="point1"></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></s:submit> </s:form>
对应PointAction.java也进行相应的改变,有一个List类型的point变量,用于存放从客户端转来的3个Point
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 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; } public List<Point> getPoint() { return point; } public void setPoint(List<Point> point) { this.point = point; } public String execute() throws Exception { return SUCCESS; } }
PointConvert也进行相应的改变
public class PointConvert extends StrutsTypeConverter { public Object convertFromString(Map context, String[] values, Class toClass) { List<Point> list = new ArrayList<Point>(); for (String value : values) { Point point = new Point(); String[] param = value.split(","); point.setX(Integer.parseInt(param[0])); point.setY(Integer.parseInt(param[1])); list.add(point); } return list; } public String convertToString(Map context, Object o) { List<Point> list = (List<Point>)o; StringBuilder sb = new StringBuilder(); for(Point point: list){ sb.append("x= ").append(point.getX()).append(" y=").append(point.getY()); } return sb.toString(); } }