STRUTS2 type conversions can be used based on the presence of OGNL expressions, Defaulttypeconverter implements Ognl.typeconverter interfaces in Ognl, which provides a simplified convertvalue approach.
The Xwork-core package also has an interface named TypeConverter, the Strutstypeconverter class is an abstract class, inheriting Defaulttypeconverter, which defines 2 abstract methods:
converfromstring,convertostring
here are 2 test demos
Package test1;
Import Com.opensymphony.xwork2.ActionSupport;
public class Form1action extends Actionsupport {private point point;
Public Point GetPoint () {return point;
The public void SetPoint (point point) {this.point = point;
} @Override Public String execute () throws Exception {System.out.println (point);
return Super.execute ();
}} package test1;
public class Point {private Double X;
private 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;
@Override public String toString () {return ' point [x=] + x + ", y=" + y + "]";
}} package test1;
Import Java.util.Map;
Import Org.apache.struts2.util.StrutsTypeConverter; public class Pointconversion extends strutstypeconverter{@Override public Object convertfromstring (Map context, String [] values, Class toclass) {//TODO auto-generated Method Stub//Get the value passed ins, note is an array of String str = values[0];
String[] p = str.split (",");
Point point = new Point ();
Point.setx (Double.parsedouble (p[0));
Point.sety (Double.parsedouble (p[0));
return point; } @Override Public String converttostring (Map context, object o) {//object O is the object that needs to be processed.
) O;
return point.tostring ();
}
}
Input coordinates:
after you have customized the type converter, you must also configure the
1. The type converter applied to the global scope needs to create a property file named Xwork-conversion.properties at the root of the classpath, with the content: full name of the conversion class = Full name of the Type translator class (Java.util.Date = ....... );
2. Type converters that apply to specific classes
To specify a type converter to be applied to a particular class, you need to create a property file named Classname-conversion.properties in the same directory of a particular class with the content: property name of a particular class = Full name of the type converter class