struts2学习笔记四(第4讲.Struts2的类型转换续)_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > struts2学习笔记四(第4讲.Struts2的类型转换续)

struts2学习笔记四(第4讲.Struts2的类型转换续)

 2011/1/7 8:29:41  zhaolianyang  http://zhaolianyang.javaeye.com  我要评论(0)
  • 摘要:struts2中的局部类型转换新的需求:用户需要同时增加三个点的坐标。一、找到之前创建的input.jsp页面,再增加两个textfield,用于输入新增的两个点的坐标:<body><h3><fontcolor="red">使用逗号将点的两个坐标分隔开</font></h3><s:formaction="pointConverter"><s:textfieldname="point"label="point">
  • 标签:笔记 学习 类型转换 学习笔记 Struts2学习笔记 struts
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>

二、在之前创建的类PointAction添加Point类型的两个属性:
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;
	}
	
	@Override
	public String execute() throws Exception 
	{
		return SUCCESS;
	}
}


三、找到之前创建的属性文件:PointAction-conversion.properties
,同样的要增加两个类型转换的映射:
point=com.test.converter.PointConverter
point2=com.test.converter.PointConverter
point3=com.test.converter.PointConverter

四、找到之前的结果页面output.jsp:
<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>

效果图如下:








struts2中的全局类型转换
一、在src目录下创建xwork-conversion.properties文件(全局的属性文件):
com.test.bean.Point=com.test.converter.PointConverter

注释掉PointAction-conversion.properties
中的内容如下:
#point=com.test.converter.PointConverter
#point2=com.test.converter.PointConverter
#point3=com.test.converter.PointConverter

重新运行的结果是和上面的局部运行的结果一致。

二、在com.test.converter包中创建PointConverter2.java类继承自StrutsTypeConverter类:
package com.test.converter;

import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

import com.test.bean.Point;

public class PointConverter2 extends StrutsTypeConverter {

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

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

三、同时修改xwork-conversion.properties文件中的配置信息:
com.test.bean.Point=com.test.converter.PointConverter2

增加新的需求,在PointAction类中声明一个List集合类型对象Point:
一、修改PointAction类中的代码:
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;
	}
	
	@Override
	public String execute() throws Exception 
	{
		return SUCCESS;
	}
}


二、修改输入页面input.jsp:
<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>

三、在com.test.converter包下创建PointConverter3.java类:
package com.test.converter;

import java.util.*;

import org.apache.struts2.util.StrutsTypeConverter;

import com.test.bean.Point;

public class PointConverter3 extends StrutsTypeConverter {

	@Override
	public 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;
	}

	@Override
	public 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();
	}
}



四、修改PointAction-conversion.properties:
#point=com.test.converter.PointConverter
#point2=com.test.converter.PointConverter
#point3=com.test.converter.PointConverter
point=com.test.converter.PointConverter3

五、修改注释掉这里的代码xwork-conversion.properties:
#com.test.bean.Point=com.test.converter.PointConverter3

六、修改输出页面output.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"/>
  </body>

最终显示效果:


发表评论
用户名: 匿名