Set Method of Java. util. Date (struts2 type conversion)

Source: Internet
Author: User

The Set Method of Java. util. date does not seem to have much to do with the struts2 type conversion, but in some cases, java. util. date needs to be converted to be set, especially when struts2 is used.

This is the case. The project is in the testing phase, and a bug is a headache for us. The date selected by the my97 date plug-in on the page. In the MAC system, FF is passed to the background and cannot be set (other browsers in other systems are normal ...). During the study, the server reported that the setbirthday ([ljava. Lang. String birthday) of this attribute could not be found, but in fact, birthday defines java. util. Date.

/*** Birthday */private Java. util. date birthday; Public Java. util. date getbirthday () {return birthday;} public void setbirthday (Java. util. date birthday) {This. birthday = birthday ;}

In desperation, I wrote a stirng type set method.

public void setBirthday(String birthday) {Date date = null;if(!StringUtils.isEmpty(birthday)){try {date = DateUtil.convertStringToDate(birthday);} catch (ParseException e) {e.printStackTrace();}}this.birthday = d1;}

In this case, the Mac machine FF accesses the Windows development machine. After the problem is found, it is to be deployed on the testing machine (Linux), and setbirthday (string) cannot be found )... Surprisingly, it is normal to mention setbirthday (string) before setbirthday (Java. util. date.

This bug is fixed, but it always feels weird. Later, I talked to my colleagues about the possible problem of struts2 data conversion. I added the data conversion (remove setbirthday (string) separately, which is actually normal. I simply added both solutions.

#DateConverterjava.util.Date=org.abc.commons.util.DateTypeConverter

Xwork-conversion.properties, placed under the src directory.

Public class datetypeconverter extends defaulttypeconverter {@ overridepublic object convertvalue (MAP context, object value, class totype) {simpledateformat = new simpledateformat ("yyyy-mm-dd "); if (totype = date. class) {// when the browser submits a request to the server, convert string to date = NULL; string datestring = NULL; string [] Params = (string []) value; datestring = Params [0]; // obtain the date string try {date = simpledateformat. parse (datestring); // supports the format for traversing the date to convert return date;} catch (exception e) {} return NULL;} else if (totype = string. class) {// when the server outputs data to the browser, convert the date to string type to date = (date) value; return New simpledateformat ("yyyy-mm-dd "). format (date); // The output format is yyyy-mm-dd} return NULL ;}}

It can also be seen that struts2 accepts parameters in arrays, and the above ([ljava. Lang. String birthday) is not hard to understand.

Struts2 data converter see: http://www.cnblogs.com/lpshou/archive/2012/11/27/2791188.html

Related Article

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.