Processing of calendar and date in struts2

Source: Internet
Author: User
Tags dateformat

When reading the struts2 document, I tried type conversion, but unfortunately there is always an error message, as shown below:

Reference Error
[Parametersinterceptor] parametersinterceptor-[setparameters]:
Unexpected exception caught setting 'datainfo. birthday' on 'class
Com. chsi. sample. Action. sampleeaction: Error setting expression
'Datainfo. birthday' with value' [ljava. Lang. String; @ 13ac14a'

SimpleCodeAs follows:

Reference public class sampleeaction extends basiccrutsupportaction implements preparable {

Private sampleobject datainfo;

// Other code

}

,

Conversion code snippet:

Java code
  1. /* 
  2. * Created on 2008-10-10 
  3. */
  4. PackageCom. chsi. sample. typeconversion;
  5. ImportJava. Text. dateformat;
  6. ImportJava. Text. parseexception;
  7. ImportJava. Text. simpledateformat;
  8. ImportJava. util. calendar;
  9. ImportJava. util. date;
  10. ImportJava. util. Map;
  11. ImportOrg. Apache. commons. Logging. log;
  12. ImportOrg. Apache. commons. Logging. logfactory;
  13. ImportOrg. Apache. struts2.util. strutstypeconverter;
  14. /** 
  15. * A simple data type conversion instance 
  16. * @ Author anwx <a href = "mailto: XXXXXX@c.com"> An weixiao </a> 
  17. * @ Version $ ID $ 
  18. */
  19. Public ClassSampletypeconversionExtendsStrutstypeconverter {
  20. PrivateLog = logfactory. getlog (sampletypeconversion.Class);
  21. @ Override
  22. PublicObject convertfromstring (MAP context, string [] values, class toclass ){
  23. Log.info ("Run convertfromstring :......");
  24. If(Values =Null| Values [0] =Null)
  25. Return Null;
  26. [Color = Red] Calendar Cal = calendar. getinstance (); [/color]// Error: Cal = NULL
  27. Dateformat =NewSimpledateformat ("Yyyy-mm-dd");
  28. Try{
  29. Date = dateformat. parse (Values [0]);
  30. Cal. settime (date );
  31. }Catch(Parseexception e ){
  32. Log.info ("Typeconversion Error !!! ");
  33. E. printstacktrace ();
  34. ReturnCal;
  35. }
  36. ReturnCal;
  37. }
  38. @ Override
  39. PublicString converttostring (MAP context, object ){
  40. Log.info ("Run converttostring :......");
  41. If(ObjectInstanceofCalendar ){
  42. Stringbuffer buffer =NewStringbuffer ();
  43. Calendar o = (calendar) object;
  44. Buffer. append (O. Get (calendar. Year ));
  45. Buffer. append ("-"). Append (O. Get (calendar. month) +1);
  46. Buffer. append ("-"). Append (O. Get (calendar. Date ));
  47. ReturnBuffer. tostring ();
  48. }Else{
  49. Log.info ("Non-calendar type");
  50. Return Null;
  51. }
  52. }
  53. /* (Non-javadoc) 
  54. * @ See org. Apache. struts2.util. strutstypeconverter # convertvalue (Java. util. Map, java. Lang. Object, java. Lang. Class) 
  55. */
  56. @ Override
  57. PublicObject convertvalue (MAP arg0, object arg1, class arg2 ){
  58. Return Super. Convertvalue (arg0, arg1, arg2 );
  59. }
  60. /* (Non-javadoc) 
  61. * @ See org. Apache. struts2.util. strutstypeconverter # define mfallbackconversion (Java. util. Map, java. Lang. Object, java. Lang. Class) 
  62. */
  63. @ Override
  64. ProtectedObject implements mfallbackconversion (MAP arg0, object arg1, class arg2 ){
  65. Return Super. Required mfallbackconversion (arg0, arg1, arg2 );
  66. }
  67. }

Then the SampleEAction-conversion.properties file content is:

Datainfo. Birthday = com. chsi. sample. typeconversion. sampletypeconversion

Note that the red part in conversion is correct, and the error message is mainly caused by the null Cal, because it is a newbie, I think the conversion configuration is wrong ......, It's just speechless ......



Of course, when processing conversion, date and calendar ar are also considered. From the document provided by struts2, if you want to display a date format on the page, you can use:

About java. util. Date:

1. <s: Text>

Reference <s: Text name = "format. date "> <s: Param name =" value "value =" datainfo. specialdate "> </S: param> </S: Text>

2. <s: Date> is more convenient.

<S: Date Format = ""/>

3. Of course, we can also get the corresponding data through the request and then directly convert it by ourselves.

If the form contains the date type that requires the format, you can use:

<S: textfield key = "datainfo. specialdate" value = "% {gettext ('format. date', {datainfo. specialdate})}"/>

The formate. date comes from the property file,Note that there is no space on both sides of '=' When writing the attribute; otherwise, a space will appear in <input value = "">

Note that the second parameter of gettext should not be enclosed in quotation marks !!!, This is an error in the struts2 document ......, It is also speechless, which makes it possible for me to handle the java. util. date type for this entire conversion,

About java. util. calendar:

This attribute requires the use of conversion to complete the conversion, as long as the conversion is implemented, other problems are the conversion between date and calendar ......, On the page, we can use its attribute time to obtain date data, and the processing method is the same as date.

Value = "datainfo. MC. Time" indicates the date value of the calendar ar type.

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.