struts2-写自己的转换器

b) 写自己的转换器:
public class MyPointConverter extends DefaultTypeConverter{

	@Override
	public Object convertValue(Object value, Class toType) {
		if(toType == Point.class) {
			Point p = new Point();
			String[] strs = (String[])value;
			String[] xy = strs[0].split(",");
			p.x = Integer.parseInt(xy[0]);
			p.y = Integer.parseInt(xy[1]);
			return p;
		}
		if(toType == String.class) {
			return value.toString();
		}
		return super.convertValue(value, toType);
	}

}
public class MyPointConverter extends StrutsTypeConverter{



	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		
			Point p = new Point();
			String[] strs = (String[])values;
			String[] xy = strs[0].split(",");
			p.x = Integer.parseInt(xy[0]);
			p.y = Integer.parseInt(xy[1]);
			return p;
		
		
	}

	@Override
	public String convertToString(Map context, Object o) {
		// TODO Auto-generated method stub
		return o.toString();
	}

}


c) 三种注册方式:i. 局部:XXXAction-conversion.properties
1. p(属性名称) =  converter
ii. 全局:xwork-conversion.properties
1. com.xxx.XXX(类名)= converter
iii. Annotation
d) 如果遇到非常麻烦的映射转换i. request.setAttribute();
ii. session


package com.hugui.action;

import java.awt.Point;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {
	
	private String name;
	private int age;
	private Date d;
	//传值:interests=example
	Set<String> interests;
	//传值:user['key']=value
	Map<String, String> users;
	//传值:p.x=4&p.y=5
	Point p;
	
	List<Point> ps;
	Map<String, Point> points;
	
	
	@Override
	public String execute() throws Exception {
		
		return super.execute();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getD() {
		return d;
	}

	public void setD(Date d) {
		this.d = d;
	}

	public Set<String> getInterests() {
		return interests;
	}

	public void setInterests(Set<String> interests) {
		this.interests = interests;
	}

	public Map<String, String> getUsers() {
		return users;
	}

	public void setUsers(Map<String, String> users) {
		this.users = users;
	}

	public Point getP() {
		return p;
	}

	public void setP(Point p) {
		this.p = p;
	}

	public List<Point> getPs() {
		return ps;
	}

	public void setPs(List<Point> ps) {
		this.ps = ps;
	}

	public Map<String, Point> getPoints() {
		return points;
	}

	public void setPoints(Map<String, Point> points) {
		this.points = points;
	}

	

}



package com.bjsxt.converter;

import java.awt.Point;
import java.util.Map;

import org.apache.struts2.util.StrutsTypeConverter;

public class MyPointConverter extends StrutsTypeConverter{



	@Override
	public Object convertFromString(Map context, String[] values, Class toClass) {
		
			Point p = new Point();
			String[] strs = (String[])values;
			String[] xy = strs[0].split(",");
			p.x = Integer.parseInt(xy[0]);
			p.y = Integer.parseInt(xy[1]);
			return p;
		
		
	}

	@Override
	public String convertToString(Map context, Object o) {
		// TODO Auto-generated method stub
		return o.toString();
	}

}




如果遇到不能转换的类型,就到属性文件里面去看有没有转换器
1. p(属性名称) =  converter
ii. 全局:xwork-conversion.properties
1. com.xxx.XXX(类名)= converter
iii. Annotation
java.awt.Point=com.bjsxt.converter.MyPointConverter


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Type Conversion</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  	name:<s:property value="name"/><br/>
  	age:<s:property value="age"/><br/>
  	date:<s:property value="d"/><br/>
  	<s:date name="d" format="yyyy/MM/dd HH:mm:ss"/><br/>
  	<s:property value="interests"/><br/>
  	<s:property value="users"/><br/>
  	<s:property value="p"/><br/>
  	<s:property value="ps"/><br/>
  	points:<s:property value="points"/><br/>
  	
  </body>
</html>

你可能感兴趣的:(java,apache,html,jsp,struts)