`

Strtus2(3):struts2的类型转换

阅读更多

1,新建一个Point类,包含两个变量x,y。只有set,get方法

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;
	}
	
}

 2,新建input.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<html> 
  <body>
  	<!-- 1,用户输入合法的Point类型的数据后,根据struts.xml文件的配置,会跳转到PointAction.java中处理 -->
  	<!-- 2,PointAction在setPoint时发现在action目录下有一个PointAction-conversion.properties文件,
  	<!--	此时Struts知道会到PointConverter类中去做类型转换,变成 Point类型 -->
  	<!-- 3,转换完成后,执行PointAction类中的execute转到output.jsp页面,output.jsp取PointAction页面的getPoint方法的返回值 -->
  	<!-- 4,在得到返回值前,需要先到PointConverter方法再做类型转换,变成String类型 -->
  	<!-- 5,如何确认哪个JavaBean对应哪个转换器?这是在xwork-conversion.properties中设置的 -->
  	<!-- 6,如果想给某个实例指定特定的转换器,就在同个包目录下,建立 类名+"-conversion.properties"文件,如Point2Action-conversion.properties -->
    <s:form action="pointConvert">
    	<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></s:submit>
    </s:form>
  </body>
</html>

 

3,建立转换类

package com.test.convert;

import java.util.Map;
import ognl.DefaultTypeConverter;
import com.test.bean.Point;

public class PointConvert extends DefaultTypeConverter {
	
	/**
	 * 根据toType的类型来判断是从客户端到服务器端,还是从服务器端到客户端
	 */
	public Object convertValue(Map context, Object value, Class toType) {
		//从客户端到服务端
		if (Point.class == toType) {
			Point point = new Point();
			String[] str = (String[]) value;
			String[] paramString = str[0].split(",");
			int x = Integer.parseInt(paramString[0]);
			int y = Integer.parseInt(paramString[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 resultString = "[x=" + x + ",y=" + y + "]";
			return resultString;
		}
		return null;
	}
}

4,建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;
	}
	public String execute() throws Exception {
		return SUCCESS;
	}
}

5,建指定类型转换配置文件PointAction-conversion.properties,放在action目录下

point=com.test.convert.PointConvert

 6,配置struts.xml文件

<action name="pointConvert" class="com.test.action.PointAction">
	<result name="success">/output.jsp</result>
	<result name="input">/input.jsp</result>	
</action>

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics