How do I customize a type converter?

Source: Internet
Author: User
Tags dateformat

Question 4: How do I customize a type converter?
1). Why do I need a custom type converter? Because struts cannot automatically convert strings to reference types.
In Java, the basic types include int, double, float, Boolean, and Char. The reference types are object types, such as array and object.

2). How to define a type converter:
Strutstypeconverter class ---> inherits ognl. defaulttypeconverter --- "inherits ognl. typeconverter

I. Development type converter class: So it is enough to expand the strutstypeconverter class.

1 package COM. atguigu. struts2.converter; 2 3 Import Java. SQL. date; 4 Import Java. text. dateformat; 5 import Java. text. parseexception; 6 Import Java. text. simpledateformat; 7 Import Java. util. map; 8 9 Import Org. apache. struts2.util. strutstypeconverter; 10 11 public class testtypeconverter extends strutstypeconverter {12 13/* 14 * This method is used to convert the passed string array into an object 15*16*17 */18 dateformat = new s Impledateformat ("YYYY-MM-DD hh: mm: SS"); 19 public object convertfromstring (MAP arg0, string [] arg1, class arg2) {20 if (arg2 = date. class) {21 if (arg1! = NULL & arg1.length> 0) {22 string = arg1 [0]; 23 try {24 dateformat. parseobject (string); 25} catch (parseexception e) {26 // todo auto-generated catch block27 E. printstacktrace (); 28} 29} 30} 31 32 return arg1; 33} 34 35/* 36*37 * this method is called when it is displayed back and forth, convert an object to a string 38*39 */40 41 Public String converttostring (MAP arg0, object arg1) {42 // determine whether it is a date object 43 If (arg1 instanceof date) {44 // Yes, use the format () method to convert the date object into a string 45 return dateformat. format (arg1); 46} 47 return NULL; 48} 49 50}

 

Ii. configuration type converter:
There are two methods
①. Field-Based Configuration:
> Create a New ModelClassName-conversion.properties file under the model (possibly action, possibly a JavaBean) where the field is located

> In this file, enter the key-Value Pair fieldname = full Class Name of the type converter.

Fieldname = the full Class Name of the type converter, for example: birth = com. atguigu. struts2.converter. testtypeconverter

> The instance is created when the converter is used for the first time.
> The type converter is for a single instance!



②. Type-Based Configuration:
> New xwork-conversion.properties under SRC

> Type: type to be converted = full Class Name of the type converter.

Type to be converted = Name of the entire converter class, for example: Java. util. Date = com. atguigu. struts2.converter. testtypeconverter

> Create an instance when the current struts2 application is loaded. therefore, if a web is referenced in the converter. pay attention to the parameters in XML, because the converter has already created an instance during struts2 loading, and the Web. XML hasn't been loaded yet, so you reference the web in the converter. the XML resource is empty, so make sure to make a judgment if it is not empty. Whether or not the converter is still ineffective

-->

How do I customize a type converter?

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.