STRUTS2 Custom type converter: Defaulttypeconverter

Source: Internet
Author: User
Tags dateformat

One, problem derivation
     Struts2 type conversions are based on OGNL expressions, because the requested arguments are strings, and Java itself is a strongly typed language. This requires that the request parameter string be converted to another type. The
    Struts2 type converter needs to implement a TypeConverter interface that is located inside the Ognl.jar package and is used OGNL technology for the STRUTS2 framework converter. The interface defines a Convertvalue () method that the type converter implementation class that implements the interface needs to override for type conversion. OGNL also provides a class defaulttypeconverter that implements the TypeConverter interface, and developers can develop the implementation classes of type converters as long as they inherit the class.

Two, custom type converter step
   1. You implement your own type converter (overriding the Convertvalue method) first by implementing the Typecoverter interface or by inheriting the Defaulttypeconverter implementation class, which implements the Typecoverter interface.      
     because the Typecoverter interface is too complex, the OGNL project also provides a class that implements the interface:D Efaulttypeconverter.
  such as:

Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import Java.util.Map; Import Ognl.
Defaulttypeconverter; public class Dateconversion extends Defaulttypeconverter {@Override @SuppressWarnings (' rawtypes ') public Object Conve
		Rtvalue (Map context, Object value, Class totype) {SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd"); try{if (ToType = = Date.class) {//when the string is converted to the Date type, then the source of value is from the URL or//person submit string[] params = (string[]) value ;//Request.getparametervalues (), convert/to string array because a component like a check box can be committed is an array return Dateformat.parse (Params[0]); Date format (Note: the date here must be the Util package)}else if (ToType = = String.class) {//when date is converted to a string, then value is derived from the//action class, so value is not strongly converted to an array
	            form (note: Using an EL expression to output to the page is that if it will not be//executed, and only if you use the OGNL expression to output to the page will enter the IF statement) Date date = (date) value; return Dateformat.format (date);//Convert to String} catch (Java.text.ParseException e) {//TODO auto-generated
		Catch block E.printstacktrace (); } RETurn null; }

}


Where the context is the contexts of the type transformation environment, value is the parameter that needs to be converted, and the ToType is the converted target type.


2. Register the converter in a Web application so that the Struts 2 framework can use that type of converter.
There are 3 types of registration for type converters:
(1) Register the local type converter: it only works on the properties of an action.
(2) Registers a converter of the global type: valid for all action.
(3) Use JDK1.5 annotation to register the converter.
Local type converter:
Create a new actionname-conversion.properties in the package under the action class that needs to take effect, where ActionName is the class name of the action that needs to be converted, followed by the-conversion fixed.
Content: The name of the property in the action to be converted = The full class name of the type converter. Note: The type converter is required to add a package name, and the last can not add punctuation
such as: date=com. Datetypeconvert

Global type converter:
Create a new Xwork-conversion.properties file in the web-inf/classes or src directory. The file name is fixed. The "note" Xwork represents the global
Content: Types to be converted = Full class names of type converters
Xwork-conversion.properties:

Java.util.date=com. Datetypeconvert

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.