Struts2 type conversion & amp; Date

Source: Internet
Author: User

Struts2 type conversion & date

The basic types in Struts can be converted by struts's default type converter .. the default format of the day type is February 03, 1992. If we enter January 1, the name will report an error... so we need to convert the type...

1. Local type converter:

Development steps:

(1) Write A type converter to inherit the StrutsTypeConverter class.

(2) Write a configuration file and ActionName-conversion.properties under the same directory of the class to be converted, as I want to convert here is the date type in the UserAction class...

(3) In this configuration file, write the attribute to be converted = the full name of the type converter.

User. date = cn. itcast. converter. MyConverter

2. Global type converter

(1) Write A type converter to inherit the StrutsTypeConverter class

(2) Write a configuration file named xwork-conversion.properties under src

(3) content:

Java. util. Date = cn. itcast. converter. MyConverter

Registry form: (struts configuration file omitted)

 




 

Custom type converter:

 

Package cn. itcast. converter; import java. text. dateFormat; import java. text. parseException; import java. text. simpleDateFormat; import java. util. date; import java. util. map; import org. apache. struts2.util. strutsTypeConverter; public class MyConverter extends StrutsTypeConverter {@ Overridepublic Object convertFromString (Map context, String [] values, Class toClass) {DateFormat [] df = {new SimpleDateFormat (yyyy-MM -Dd), new SimpleDateFormat (MM dd, yyyy), new SimpleDateFormat (yyyyMMdd)}; // determines whether it is nullif (values = null | values. length = 0) {return null;} // determines if it is of the Date type if (Date. class! = ToClass) {return null;} for (int I = 0; I
 
  
UserAction: to convert to the user. date attribute in this class
  

 

 

package cn.itcast.type;import cn.itcast.domain.User;import com.opensymphony.xwork2.ActionSupport;public class UserAction  extends ActionSupport{private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String register(){System.out.println(user.getUsername()+==+user.getAge()+==+user.getDate());return register;}}
If it is: local configuration file:

 

User. date = cn. itcast. converter. MyConverter
If it is: global configuration file

Java. util. Date = cn. itcast. converter. MyConverter

Enter the following content in the form:

Console printing:

Li Weikang = 22 = Sat Sep 12 00:00:00 CST 1992
 

 

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.