日期:2014-05-20  浏览次数:20655 次

求助:struts2类型转化的问题
我想在输入框中输入一个坐标(eg:4,5),能够将输入内容转换成Point类的对象,并且能够从服务器回到客户端时将point类的对象又转成String类型来显示,但是我写的代码只在客户端转服务器时成功了,服务器转客户端时却没有执行代码,这是什么原因呢,请教各位大神!
Point类:
public class Point {
double x;
double y;
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}

}

Point转换类:
package com.tjw.converter;

import java.util.Map;

import com.sun.java_cup.internal.internal_error;
import com.tjw.util.Point;

import ognl.DefaultTypeConverter;

public class PointCoverter extends DefaultTypeConverter{

@Override
public Object convertValue(Map context, Object value, Class toType) {
if (Point.class == toType) {
Point point = new Point();
String [] valueString = (String [])value;
String [] paramValueString = valueString[0].split(",");
String xString = paramValueString[0];
String yString = paramValueString[1];
double x = Double .parseDouble(xString);
double y = Double .parseDouble(yString);
point.setX(x);
point.setY(y);
System.out.println("to point");
return point;
}
if (String.class == toType) {
Point point = (Point)value;
String string = "[x="+point.getX()+",y="+point.getY()+"]";
System.out.println(string);
return string;
}
return null ;
}

}


PointAction类:
package com.tjw.action;

import com.opensymphony.xwork2.ActionSupport;
import com.tjw.util.Point;

public class PointAction extends ActionSupport {

private Point point ;



public Point getPoint() {
return point;
}

public void setPoint(Point point) {
this.point = point;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("1");
System.out.println("&&&&&&&&&"+point.getX());
return SUCCESS;
}
}

PointAction-conversion.properties文件(和PointAction 在一个包下):
point=com.tjw.converter.PointCoverter


struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="Point" class="com.tjw.action.PointAction">

<result name="success" >/b.jsp</result>

</action>



</package>
</struts>



a.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'a.jsp' starting page</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>
<s:form action="Point">
<s:textfield label="point" name="point"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>


b.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="x" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'b.jsp' starting page</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>
  point:<s:property value="point"/><br>
  </body>
</html>
输出结果有PointCoverter类里面的 to point,却没有to string

------解决方案--------------------
问题最多,结贴率最低,我们欠你的?