Struts (type converters)

Source: Internet
Author: User

1. Struts2 type conversion, for 8 native data types and common types such as date,string, STRUTS2 can use built-in type converters for automatic conversions, but for custom type conversions,

You need to specify how you want the type to be transferred.

2. The type converter must inherit Defaulttypeconverter, which generally inherits its subclass Strutstypeconverter. The type conversion method is called when the set and get methods are called in the action.

3. steps to implement a custom type converter:

1). Inherit Strutstypeconverter

2). Cover convertfromstring and ConvertToString

Example:

public class Utildateconverter extends Strutstypeconverter {

private static final String PATTERN = "YYYY/MM/DD";

Browser commits to server is called, converts string to object

@Override

Public Object convertfromstring (Map context, string[] values, Class toclass) {

Values are an array of properties to convert, possibly with more than one name, so take the first one.

String datestring = values[0];

SimpleDateFormat SDF = new SimpleDateFormat (PATTERN);

Date date = null;

try {

String, converted to a date type.

Date = Sdf.parse (datestring);

} catch (ParseException e) {

E.printstacktrace ();

}

return date;

}


Called when the server outputs to the browser, converting a type to a string. O is the object to convert to a string.

@Override

Public String converttostring (Map context, Object o) {

Date date = (date) O;

return new SimpleDateFormat ("yyyy mm month DD Day"). Format (date);

}

}
3). Register type Converter

a). Local type converters, local type converters only work on the current action and need to provide the following configuration file:

Myactionname-conversion.properties,myactionname refers to the use of the converter's action name "-conversion.properties" fixed string, cannot be modified.

The configuration file name of our Adduseraction type converter is called: adduseraction-conversion.properties, which must be placed in the same directory as the action, which

The format of the file is: the property name in action = The full path of the converter, such as: Birthday=com.bjpowernode.struts2.utildateconverter can also convert an array,

The set and get methods are also required.

b). Global type converters, global type converters can work on all actions (type converters with Struts1) and need to provide the following configuration file:

Xwork-conversion.properties (the name is fixed), the configuration file needs to be placed under SRC, the format of the configuration file: The full path of the type that needs to be converted = converter,

such as: Java.util.date=com.bjpowernode.struts2.utildateconverter custom types can also be converted.

Fill: Request page:

Date of birth: <input type= "text" name= "Birthday" ><br>

Display page: <s:property value= "Birthday"/> or <s:text name= "date" ></s:text>

4. If the global type converter and the local type converter exist at the same time, local preference.

5. The converttostring of the converter can be called when the struts2 tag is used to read the Property Get method. The use of Jstl is not called, and the default format is displayed in time.

6. type conversion, if an exception occurs, the Fielderror will automatically add the exception, in English.

7. Use the type converter to provide the set and get methods of the property in the action

Struts (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.