Struts Learning type Conversion

Source: Internet
Author: User

* Get the corresponding content from the page
* In action class action, declare a property with the same name as the value of the form Name property on the page
* provides get and set methods
* The STRUTS2 framework takes the corresponding content from the page through a reflection mechanism
* The STRUTS2 framework cannot convert a string type obtained from a page into any type
* Custom type converters are required when the STRUTS2 framework does not convert the string types obtained in the page
* Custom type converters:
* Either implement the TypeConverter interface or inherit an implementation class from the TypeConverter interface, we inherit the Defaulttypeconverter class
* Override Convertvalue (Object value, Class totype) {} method
* parameter "value": the value to convert
* parameter "ToType": Type to convert
* Specific code implementation:
if (value==null) {
return false;
            }
if (totype==null) {
return false;
            }
if (totype!=java.util.date.class) {
return false;
            }
if (value instanceof java.lang.string[]) {
String [] str = (string[]) value;
if (str[0]!=null&&str[0].length () >0) {
try {
SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy/mm/dd");
return Sdf.parse (Str[0]);
} catch (ParseException e) {
                        /*
* In the STRUTS2 framework, the custom type converter,
* If we do not throw exceptions manually, the STRUTS2 framework catches only exceptions, but does not throw them.
* So the framework will assume that the conversion of the type converter succeeds to the successful page.
                         */
throw new RuntimeException (e);
                    }
                }
            }
* Two ways of registering:
* Field-based (local)
* Create a resource file named "Useraction-conversion.properties" in the same directory as Action class action
* Useraction is the name of the Action class action
* "-conversion.properties" is a fixed notation
* Its content is configured as follows:
Createtime=cn.swust.converter.dateconverter(Field name = Full class name of the custom converter)
* Class-based (global)
* Under the SRC directory, create a resource file named "Xwork-conversion.properties"
* The name of the resource file is fixed
* Its content is configured as follows:
Java.util.date=cn.swust.converter.dateconverter
* If you enter an incorrect value in the page, do not manually throw an exception, the page will still be turned to a successful page
            /*
* In the STRUTS2 framework, the custom type converter,
* If we do not throw exceptions manually, the STRUTS2 framework catches only exceptions, but does not throw them.
* So the framework will assume that the conversion of the type converter succeeds to the successful page.
             */
* Configuration Modification error message is Chinese
* Create a resource file named "Converter.properties" in the same directory as Action class action
* The resource file is configured as follows:
* For all fields:
xwork.default.invalid.fieldvalue= type conversion failed ' {0} '.
* For a field:
invalid.fieldvalue.createtime= Date of birth conversion failed
* Configuration in Struts.xml file:
<constant name= "Struts.custom.i18n.resources"
value= "Cn.swust.converter.converter" >
</constant>

Struts Learning type Conversion

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.