Struts2 custom local type converter

Source: Internet
Author: User

I. AddUAction
Package com;
Import java. util. Date;
Public class AddUAction {
Private Date birdate;
Public Date getBirdate (){
Return birdate;
}
Public void setBirdate (Date birdate ){
This. birdate = birdate;
}
Public String addU (){
System. out. println (birdate );
Return "OK ";
}
}
Ii. struts. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.1 // EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<Struts>
<Package name = "te" namespace = "/te" extends = "struts-default">
<Action name = "teMan" class = "com. AddUAction" method = "addU">
<Result name = "OK">/index. jsp </result>
</Action>
</Package>
</Struts>

Iii. DateConverter
Package com;
Import java. util. Date;
Import java. text. SimpleDateFormat;
Import java. util. Map;
Import com. opensymphony. xwork2.conversion. impl. DefaultTypeConverter;
Public class DateConverter extends DefaultTypeConverter {
@ Override
Public Object convertValue (Map <String, Object> context, Object value,
Class toType ){
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyyMMdd ");
Try {
If (toType = Date. class ){
String [] params = (String []) value;
Return dateFormat. parse (params [0]);
} Else {
Date date = (Date) value;
Return dateFormat. format (date );
}
} Catch (Exception e ){
Return null;
}
}
}

4. AddUAction-conversion.properties
Birdate = com. DateConverter
5. Package Structure

 

6. Enter in the address bar
Http: // localhost: 8080/test/te/teMan! AddU? Birdate = 20010205

7. The result is displayed on the jsp page.
This is my JSP page.
Mon Feb 05 00:00:00 CST 2001
Author: hyljava

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.