Type converter, case-multi-format conversion for the struts of the SSH framework

Source: Internet
Author: User
Tags dateformat ssh

Type converters for struts (in this case a date type converter)

One, type converter
1, struts in the data submitted by the JSP (all STRINGL type), struts will be automatically converted to the corresponding type of properties in the action, (this is better than servlet,servlet need to manually convert).
2. For basic data types and date types are automatically converted.
3. The date type only supports YYYY-MM-DD format. In other formats, you need to customize the type converter.

Ii. Classification of type converters
1. Local type converter
2. Global type converter

Iii. the Struts Converter API
|–typeconverter Converter Interface
|-–defaulttypeconverter Default Type converter class
|--–strutstypeconverter User-written converter classes that need to inherit this class

Four, local type converter class
1, the local type converter development steps:
1) Write Converter class

Package sram.type;
Import Java.sql.Date;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;

Import Java.util.Map;

Import Org.apache.struts2.util.StrutsTypeConverter; /** * In project development, this class should be located under the Util package *///1. Inherit Strutstyptconverter public class Myconverter extends strutstypeconverter{//2. Implementing St Rutstypeconverter all methods in an abstract class/** * 2.1 Convert string to the specified type "string to Date" @param context Current Context * @param Values of the string submitted by the values JSP form * @param toclass the target type to convert to */public Object convertfromstring (Map context, string[ ] values, Class toclass) {try {//judgment 1: Content cannot be empty if (values==null| |
            values.length==0) {return null;
            }//Judgment 2: The type must be Date if (date.class!=toclass) {return null;
            }//convert SimpleDateFormat SDF = new SimpleDateFormat ("YyyyMMdd");
        Return Sdf.parse (Values[0]); } catch (ParseException e) {throw nEW RuntimeException (e);
    }} public String converttostring (Map arg0, Object arg1) {return null; }
}

2) Configure the converter class (tell struts to apply its own converter class)
A) Create a new properties file in the same package's action directory .
b) Naming rules: Actionclassname-conversion.properties
Example: Create useraction-conversion.properties under Sram.type package

3) Content
user.birthday= Converter class full path (Sram.type.MyConverter)

2. Summary: Can the converter be used for other action?
No, the converter here refers to the converter class (Myconverter) + config file (useraction-conversion.properties), so it cannot be used for other actions. The converter Class (Myconverter) is generic.

V. Global type converter: You need to write a converter for all the action.
1. Global type converter Development steps:
1) Configuring the Global type converter: src/xwork-conversion.properties
2) Content:
Full name of the type to convert (java.util.Date) = Type converter Class (Sram.type.MyConverter)

Vi. Summary: When a local type converter and a global type converter are present in the project, the local type converter (nearest principle) is performed preferentially.

Seven, the classic case:

Package sram.type;
Import Java.sql.Date;
Import Java.text.DateFormat;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;

Import Java.util.Map;

Import Org.apache.struts2.util.StrutsTypeConverter; public class Myconverter extends strutstypeconverter{/** * Requirements: The format required for project support is: * YYYY-MM-DD * yyy YMMDD * yyyy mm month DD Day * ... *////First define the format of the supported transformations in the project dateformat[] df = {New Simpleda

    Teformat ("Yyyy-mm-dd"), New SimpleDateFormat ("YyyyMMdd"), New SimpleDateFormat ("YYYY year MM DD Day"),}; Public Object convertfromstring (Map context, string[] values, Class toclass) {//Judgment 1: Content cannot be empty if (values== null| |
        values.length==0) {return null;
        }//Judgment 2: The type must be Date if (date.class!=toclass) {return null;
                }//Iteration: Conversion failed to continue the conversion of the next format; the conversion succeeds directly returns the for (int i=0;i<df.length;i++) {try {///transform return Df[i]. Parse (values[0]);
            } catch (ParseException e) {continue;
    }} return null;
    Public String converttostring (Map arg0, Object arg1) {return null; }
}

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.