STRUTS2 Custom Type Converters

Source: Internet
Author: User

The data sent by the client is saved in the form of string[]. In Struts2, there are only very few data type conversions supported, such as native data types. There are also common string or date types. Struts cannot know which data type we will use. So it provides some classes that implement custom type conversions. The specific conversion process is as follows:

First we want to define the type of data we need. For example, define a user type. The user class code is as follows:

 PackageCom.struts2.customtype; Public classUser {PrivateString name; PrivateString surname;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getsurname () {returnsurname; }     Public voidSetsurname (String surname) { This. Surname =surname; }    }

Then define the action class to handle the request:

 Packagecom.struts2.customaction;ImportCom.opensymphony.xwork2.ActionSupport;ImportCom.struts2.customtype.User; Public classUseractionextendsactionsupport{Privateuser User;  PublicUser GetUser () {returnuser; }     Public voidsetUser (user user) { This. user =user; } @Override PublicString Execute ()throwsException {//TODO auto-generated Method Stub                returnactionsupport.success; }    }

Then we'll define a custom type converter, which needs to inherit a class called: Defaulttypeconverter class. It is provided by OBGL this package. We're going to rewrite the Convertvalue method inside, and this method will be called by struts. The code is as follows:

 Packagecom.struts2.conversion;ImportJava.lang.reflect.Member;ImportJava.util.Map;ImportJava.util.StringTokenizer;ImportCom.struts2.customtype.User;ImportOGNL. Defaulttypeconverter; Public classUserconversionextendsdefaulttypeconverter{@Override PublicObject Convertvalue (Map context, Object target, Member Member, String PropertyName, Object value, Class ToType ) {    //TODO auto-generated Method Stub    if(Totype==user.class) {string[] param=(string[]) value; StringTokenizer St=NewStringTokenizer (Param[0], ";"); String name=St.nexttoken (); String Surname=St.nexttoken (); User User=NewUser ();        User.setname (name);                User.setsurname (surname); returnuser; }    Else if(totype==string.class) {User User=(User) value; return"Your name is:" +user.getsurname () + "" +User.getname (); }    return NULL;}}

I have implemented the conversion process in the Convertvalue method, that is, to determine if the string type is divided into two parts, part of the user name, part of the password. The split two string is then assigned to a new user object. Then return the user object back. This enables a transformation.

The code behind the same principle.

So we've written a type converter of our own.

Now we have written three classes. These three classes still have nothing to do with it. So how do you relate them? Struts let's build a properties file under the custom action package. The name of this file is required. File name is:

Then we fill in this file with the corresponding key value pair, Field=actionconvert. Class. field is the name of the property to be converted in your action. Actionconvert. Class is the converter class that you write yourself. Save after you finish writing. This type of conversion process is complete!

As follows:

STRUTS2 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: 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.