struts2-写自己的转换器
b)写自己的转换器:
public class MyPointConverter extends DefaultTypeConverter{@Overridepublic 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{@Overridepublic 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;}@Overridepublic String convertToString(Map context, Object o) {// TODO Auto-generated method stubreturn o.toString();}}
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=exampleSet<String> interests;//传值:user['key']=valueMap<String, String> users;//传值:p.x=4&p.y=5Point p;List<Point> ps;Map<String, Point> points;@Overridepublic 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{@Overridepublic 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;}@Overridepublic String convertToString(Map context, Object o) {// TODO Auto-generated method stubreturn o.toString();}}
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>