package com.action;import java.util.Date;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class ActionInfo extends ActionSupport{
private point point;
private String username;
private int age;
private Date brithday;
public point getPoint() {
return point;
}
public void setPoint(point point) {
this.point = point;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
@Override
public String execute() throws Exception {

System.out.println("请求leaction ");
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("point", point);
 return SUCCESS;
}
 
// public void validate() {
// // TODO Auto-generated method stub
// super.validate();
} package com.action;import java.util.Map;import org.apache.struts2.util.StrutsTypeConverter; public class converter extends StrutsTypeConverter{ @Override
public Object convertFromString(Map arg0, String[] str, Class arg2) {
String[] string =str[0].split("-");
//  String[] string=arg1[0].split("-");
 point point = new point();
 point.setX(Integer.parseInt(string[0]));
 point.setY(Integer.parseInt(string[1]));
 point.setZ(Integer.parseInt(string[2]));

return point;
} @Override
public String convertToString(Map arg0, Object ob) {
        
point point= (point)ob;
int x= point.getX();
int y= point.getY();
int z = point.getZ();
     return "[x="+x+","+"y="+y+","+"z="+z+"]";



}}
package com.action;public class point {
private int x;
private int y;
private int z;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getZ() {
return z;
}
public void setZ(int z) {
this.z = z;
}
// public String toString()
// {
// return "[x="+x+","+"y="+y+","+"z="+z+"]";
// }
}

解决方案 »

  1.   

    请大家帮我看看我的这个struts2的类型转换问题 我在jsp页面输入任何东西都是显示不是有效的字段谢谢大家
      

  2.   

    你是研究struts2之前的版本 的? 
      

  3.   

    楼主你提供的信息不全,首先struts2有内置的类型转换器,可以进行int和date等到String的简单转换。默认就能用。还用,自定义的类型转换器,基于OGNL的类型转换器,你是想用哪种啊?要是基于OGNL的类型转换器,只需重写execute()方法就行了,然后再jsp页面上设置标签,struts2里配置。不是很难的。
    converter是什么意思啊?