Custom type Converters

Source: Internet
Author: User

Custom type converters must implement Ongl. TypeConverter interface or a specific implementation of this interface to extendextended Strutstypeconverter classIn most type converters, you need to provide conversion functionality from string types to non-string types and vice versa
There are two abstract methods in Strutstypeconverter: simple example: I: Creating a mytypeconverter converter is the function of converting a string into a number. Public classMytypeconverter extendsStrutstypeconverter {      @Override      PublicObject convertfromstring ( Map context, string[] values, Class toclass) {System. out. println ("Values:"+ values[0]);                     intparseint = Integer. parseint (Values[0]);           returnparseint;     }      @Override      PublicString converttostring ( Map context, Object o) {System. out. println (o);           return NULL ;     } }II: There is an attribute in the action Public classuseraction extends Actionsupport Implementspreparable,Modeldriven<user>, Requestaware {       PrivateInteger Num;      Public voidsetnum (Integer num) {System. out. println ("User NUm:"+num);           This.Num= num;     }      PublicInteger Getnum () {           returnNum;     }  }III: Create a Property file:useraction-conversion.propertiesTo edit the properties file:num=Mytypeconverter. MyType The content in the properties file is:
Property name = Full class name of the type converter
For this example, the name of the file should beuseraction-conversion.properties2: Based on class configuration (Global): Create the Xwork-conversion.properties file in the web-inf/classes/directory. "
The content in the properties file is:
type to convert = Full class name of the type converter
For the purposes of this example,xwork-conversion.propertiesThe contents of the file are:java.lang.integer=Typecovert.Mytypeconverter

Custom type Converters

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.