STRUTS2 Custom type converter detailed

Source: Internet
Author: User
Tags dateformat

STRUTS2 type conversions are based on OGNL expressions, because the requested arguments are strings, and Java itself is a strongly typed language, which requires converting strings to other types. This is done mainly through the following 2 steps:

1. Implement your own type converter (overriding the Convertvalue method) first by implementing the Typecoverter interface or inheriting the Defaulttypeconverter implementation class (which implements the Typecoverter interface)

(1) Typecoverter interface:

    Public interfacetypeconberter{public
       Object convertvalue (Map context,object target,member member,string Propertyname,object value, Class totype)  
       {}
    }

Because the Typecoverter interface is too complex, the OGNL project also provides a class that implements the interface:defaulttypeconverter.

(2) Defaulttypeconverter interface:

    public class Usertypeconvert extends Defaulttypeconverter {
      @Override the public 
      Object Convertvalue (map< String, object> context, Object value,   Class totype) {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.

Eg: date format conversion

Loginaction.java:

   public class Loginaction {
     private Date birthday;
     Public Date Getbirthday () {return
         birthday;
     } 
     public void Setbirthday (Date birthday) {
         this.birthday = birthday;
     } 
     Public String Execute () throws unsupportedencodingexception{return
         "Success";
      }
   }

Datetypeconvert.java:

   public class Usertypeconvert extends Defaulttypeconverter  { 
     @Override the public 
     Object Convertvalue (map< String, object> context, Object value, Class totype) 
     {
        SimpleDateFormat dateformat = new SimpleDateFormat (" YyyyMMdd "); 
        try{
            if (ToType = = Date.class) { 
               String [] datevalue = (string []) value; 
               Return Dateformat.parse (datevalue[0]); 
            else if (ToType = = String.class) { 
               Date DateValue = (date) value;
               Return Dateformat.format (DateValue); 
            } 
        catch (ParseException e) { 
            e.printstacktrace (); 
       }
       return null; 
     }
    }

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:

Place the actionname-conversion.properties under the package of 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.

Content: The type 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

Loginaction-conversion.properties:

    Birthday =convert. Usertypeconvert

Global type converter:

Place the Xwork-conversion.properties file under Web-inf/class. The file name is fixed.

Content: Types to be converted = Full class names of type converters

Xwork-conversion.properties:

    Java.util.Date = Convert. Usertypeconvert

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.