struts2中的类型转化 Converter

Struts.xml:   
<action name="pointConverter" class="com.liming.action.PointAction">
    		<result>/person/output.jsp</result>
    		<result name="input">/person/input.jsp</result>
    	</action>



对象:用户输入记录坐标,以,分割
public class Point {
	private int x;
	private int y;
	/**
	 * @return the x
	 */
	/**
	 * @return the x
	 */
	public int getX() {
		return x;
	}
	/**
	 * @param x the x to set
	 */
	public void setX(int x) {
		this.x = x;
	}
	/**
	 * @return the y
	 */
	public int getY() {
		return y;
	}
	/**
	 * @param y the y to set
	 */
	public void setY(int y) {
		this.y = y;
	}

}

input.jsp:

<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:form>







第一种Converter:


public class PointConverter extends DefaultTypeConverter {

	
	public Object convertValue(Map context, Object value, Class toType) {
		if(Point.class ==toType){
			System.out.println("toPoint");
			String[] str = (String[])value;
			String[] k = str[0].split(",");
			Point p = new Point();
			p.setX(Integer.parseInt(k[0]));
			p.setY(Integer.parseInt(k[1]));
			return p;
		}
		if(String.class == toType){
			System.out.println("toString");
			Point point = (Point)value;
			String s = "x="+point.getX()+" y="+point.getY();
			return s;
		}
		return null;
	}	

/**第二种Converter:
***/
public class PointConverter2 extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		String[] str = (String[])values;
		String[] k = str[0].split(",");
		Point p = new Point();
		p.setX(Integer.parseInt(k[0]));
		p.setY(Integer.parseInt(k[1]));
		return p;
	}

	@Override
	public String convertToString(Map context, Object o) {
		Point point = (Point)o;
		String s = "x="+point.getX()+" y="+point.getY();
		return s;
	}

}


================================================================================================集合类型============================================
这时候一定要定义当前类的PointAction-converter.properties,不然根据全局不会产生length STRING[]数组
jsp:
   <s:form action="pointConverter">
   		<s:textfield name="point" label="point"></s:textfield>
   		<s:textfield name="point" label="point"></s:textfield>
   		<s:textfield name="point" label="point"></s:textfield>
</s:form>



converter:
public class PointConverter3 extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		List<Point> points = new ArrayList<Point>();
		for(String str :(String[])values){
			String[] k = str.split(",");
			Point p = new Point();
			p.setX(Integer.parseInt(k[0]));
			p.setY(Integer.parseInt(k[1]));
			points.add(p);}
		return points;
	}

	@Override
	public String convertToString(Map context, Object o) {
		List<Point> points = (List<Point>)o;
		StringBuilder sf = new StringBuilder();
		sf.append("[");
		for(Point point : points){
			sf.append("x=").append(point.getX()).append(", y=").append(point.getY());
		}
		sf.append("]");
		return sf.toString();
	}
}


=======================================
xwork-converter.properties

com.liming.bean.Point=com.liming.converter.PointConverter3

你可能感兴趣的:(html,jsp,bean,xml,struts)