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
- /*
-
- * Created on 2008-10-10
-
- */
-
- PackageCom. chsi. sample. typeconversion;
-
-
- ImportJava. Text. dateformat;
-
- ImportJava. Text. parseexception;
-
- ImportJava. Text. simpledateformat;
-
- ImportJava. util. calendar;
- ImportJava. util. date;
-
- ImportJava. util. Map;
-
-
- ImportOrg. Apache. commons. Logging. log;
-
- ImportOrg. Apache. commons. Logging. logfactory;
-
- ImportOrg. Apache. struts2.util. strutstypeconverter;
-
-
- /**
-
- * A simple data type conversion instance
-
- *
- * @ Author anwx <a href = "mailto: XXXXXX@c.com"> An weixiao </a>
-
- * @ Version $ ID $
-
- */
-
- Public ClassSampletypeconversionExtendsStrutstypeconverter {
-
- PrivateLog = logfactory. getlog (sampletypeconversion.Class);
-
- @ Override
- PublicObject convertfromstring (MAP context, string [] values, class toclass ){
-
- Log.info ("Run convertfromstring :......");
-
- If(Values =Null| Values [0] =Null)
-
- Return Null;
- [Color = Red] Calendar Cal = calendar. getinstance (); [/color]// Error: Cal = NULL
-
- Dateformat =NewSimpledateformat ("Yyyy-mm-dd");
-
- Try{
-
- Date = dateformat. parse (Values [0]);
-
- Cal. settime (date );
- }Catch(Parseexception e ){
-
- Log.info ("Typeconversion Error !!! ");
-
- E. printstacktrace ();
-
- ReturnCal;
-
- }
-
- ReturnCal;
- }
-
-
- @ Override
-
- PublicString converttostring (MAP context, object ){
-
- Log.info ("Run converttostring :......");
-
- If(ObjectInstanceofCalendar ){
- Stringbuffer buffer =NewStringbuffer ();
-
- Calendar o = (calendar) object;
-
- Buffer. append (O. Get (calendar. Year ));
-
- Buffer. append ("-"). Append (O. Get (calendar. month) +1);
- Buffer. append ("-"). Append (O. Get (calendar. Date ));
-
- ReturnBuffer. tostring ();
-
- }Else{
-
- Log.info ("Non-calendar type");
-
- Return Null;
- }
-
- }
-
-
- /* (Non-javadoc)
-
- * @ See org. Apache. struts2.util. strutstypeconverter # convertvalue (Java. util. Map, java. Lang. Object, java. Lang. Class)
-
- */
-
- @ Override
- PublicObject convertvalue (MAP arg0, object arg1, class arg2 ){
-
- Return Super. Convertvalue (arg0, arg1, arg2 );
-
- }
-
-
- /* (Non-javadoc)
-
- * @ See org. Apache. struts2.util. strutstypeconverter # define mfallbackconversion (Java. util. Map, java. Lang. Object, java. Lang. Class)
- */
-
- @ Override
-
- ProtectedObject implements mfallbackconversion (MAP arg0, object arg1, class arg2 ){
-
- Return Super. Required mfallbackconversion (arg0, arg1, arg2 );
-
- }
-
- }
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.