STRUTS2 type converter uses

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.