Automatic type conversion of Struts2 and custom type conversion

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

From:

Why is there a type conversion?  

  Any transfer of the content in the HTTP protocol are both of type String, so once we on the server object, a non String type for example: int or Date, so we need to receive the HTTP requested data, the data transform String type as we need to correspond to the type of data, then use the. This process is the type conversion 

Type conversion is transparent in Struts2, namely the Struts2 built-in type conversion mechanism.  

One, overview 

  In B/S applications, string request parameters into the corresponding data type, is provided by the MVC framework, Struts2 framework is MVC very good realization, behoove, provides a type conversion mechanism.  

  Type Struts2 is based on the OGNL expression, as long as we put HTML entry (parameter form elements and other GET/POET) named OGNL expression is legal, it can make full use of conversion mechanism of Struts2.  

    In addition, the Struts2 provides good extensibility, a type converter development developers can be very simple to own, complete the conversion between strings and custom composite types. Overall, the type Struts2 converter provides a very powerful presentation layer data processing mechanism, the developer can convert mechanism to complete any type conversion using Struts2 type.  

Two types of conversion routine.: 

  For example, the form submission information with a user name, birthday, age, the corresponding data types in Action are String, Date, int. to say nothing, Struts2 will automatically. 

  Struts2 has built-in type converter is used, such as String, Boolean, char, int, long, float, double, Date, array (assuming that the element is String type), set (assuming that the element is of type String, use the ArrayList package) 

Implementing a custom type converter 

   Implement the TypeCoverter interface, or the implementation of class DefaultTypeConverter (this class implements the TypeCoverter interface), by inheriting the implementation class to realize their own type converter. You can override the convertValue method.  

  In order to simplify the implementation of a type converter, Struts2 provides a StrutsTypeConverter abstract class, the abstract class is a subclass of DefaultConverter. Implementation of the method, and provides a method for 2 different conversion direction: Object 

convertToString(Map context,String[] values,Class toClass)And String convertFromString(Map context,Object o).  

Three, registration application 

   After the implementation of custom type converter, the type converter is registered in the Web application, the Struts2 framework can be used normally the type converter.  

A type converter registration way, mainly has 3:  

A,Registered local type converter: only the attribute of an Action effect.  

B,Registration: Global type converter type specific properties of all Action will take effect.  

1,Local type converter 

Provide the following documents 

File name:   ActionName-conversion.properties  

Content: multiple propertyName (property name) = type converter class (including the package name), such as  date=com.aumy.DateConverter 

Location: and the same ActionName class path.  

2,Global type converter 

Provide the following documents 

File name:   xwork-conversion.properties 

Content:     multiple “ composite type = corresponding type converter components, such as ”  java.Util.Date=com.aumy.DateConverter 

Location: WEB-INF/classes/ directory.  

Four, application example 

Because the Struts2 on the conversion date display, can display the date and time of the project, now only need to display the date, so using a type converter to realize date display custom.  

1,Type conversion

package com.cczjjg.util;

import java.text.*;
import java.util.Date;
import java.util.Map;
import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;

public class DateTypeConverter extends DefaultTypeConverter {

    private static final DateFormat[] ACCEPT_DATE_FORMATS = {

    new SimpleDateFormat("dd/MM/yyyy"),

    new SimpleDateFormat("yyyMMdd"),

    new SimpleDateFormat("yyyy-MM-dd"),

    new SimpleDateFormat("yyyy/MM/dd") }; // To support the conversion date format

    public Object convertValue(Map context, Object value, Class toType) {

        if (toType == Date.class) { // The browser is submitted to the server, String to Date conversion

            Date date = null;

            String dateString = null;

            String[] params = (String[]) value;

            dateString = params[0];// Gets a string date

            for (DateFormat format : ACCEPT_DATE_FORMATS) {

                try {

                    return format.parse(dateString);// Ergodic date support format, conversion

                } catch (Exception e) {

                    continue;

                }

            }

            return null;

        }else if (toType == String.class) { // The server to the browser output, Date to String type conversion

            Date date = (Date) value;

            return new SimpleDateFormat("yyyy-MM-dd").format(date);// The output format is yyyy-MM-dd

        }

        return null;

    }

}



2 specific registration application 

The local configuration: 

The ActionName-conversion.properties configuration file 

1

java.util.Date=xxx.xxx.xxxx.DateTypeConverter 

The global configuration: 

The xwork-conversion.properties configuration file 


1

java.util.Date=xxx.xxx.xxxx.DateTypeConverter  

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Gladys at November 12, 2013 - 4:02 PM