首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

struts2-写自个儿的转换器

2012-09-03 
struts2-写自己的转换器b)写自己的转换器:public class MyPointConverter extends DefaultTypeConverter{@

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


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=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();}}


如果遇到不能转换的类型,就到属性文件里面去看有没有转换器
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>

热点排行