Reasons for the Nan Error in the Sx:datetimepicker label for struts 2.1.8.1 and solutions

Source: Internet
Author: User
Tags zip dcap


Author: Junsan.jin
Email: junsanjin@gmail.com
qq:1305896503

This article original address: http://www.rsky.com.cn/Article/java/201005/11748.html
Welcome to reprint, please retain the author information, thank you.

Recent projects using SPRING+STRUTS2+JPA (Hibernate) framework for development, a large number of use of the STRUTS2 tag library, really let the JSP page clean a lot, but also greatly improve the efficiency of development.

However, when you use the Sx:datetimepicker tag, you find a problem when you enter the date content, then delete the content, the input box is "", and then click on the date selected pop-up control, the discovery date all become Nan.

The following analysis causes and find solutions. If you just want to see the solution, see the Final Solution section directly.

Basic Solutions

The starting solution is to verify the format of the date, and if the date of Nan is selected, the date format is incorrect and the submission is not allowed.


Find out why

Then online check, there is no solution to the total, most of which is to say that this is a bug, before submitting a check on it. A few years ago there are bugs, it is estimated that struts also have no mind to solve, so, ready to amend themselves.

Because the sx:datetimepicker tag is used, look at the source code of the label first.

struts-2.1.8/src/plugins/dojo/src/main/java/org/apache/struts2/dojo/in the source package Components found Datetimepicker.java, and looked at just an ordinary tag library that initializes and writes out the HTML code.

So, look at the code that it eventually generates. The generated code is roughly as follows


<div dojotype= "Struts:strutsdatepicker" id= "App.begindate" value= "2010-05-13t09:27:31" name= "App.beginDate" Inputname= "Dojo.app.beginDate" displayformat= "YYYY-MM-DD" saveformat= "RFC" ></div>
<script language= "JavaScript" type= "Text/javascript" >djconfig.searchids.push ("App.begindate"); </script >

The Head Declaration section introduces


<script language= "JavaScript" type= "Text/javascript"
src= "/Application name/struts/dojo/struts_dojo.js" ></script>
Struts_dojo.js this JS.

The dojo settings are also made,


<script language= "JavaScript" type= "Text/javascript" >
Dojo Configuration
Djconfig = {
Isdebug:false,
Bindencoding: "Utf-8"
, Baserelativepath: "/dcap/struts/dojo/"
, Basescripturi: "/dcap/struts/dojo/"
, Parsewidgets:false

};
</script>


Using dojo, so, no surprise strutsdatepicker this group should be defined in the Struts_dojo.js file.

In the JS to search for Strutsdatepicker, found the


Dojo.provide ("Struts.widget.StrutsDatePicker");


and found it used again.


Dojo.widget.DropdownDatePicker this group of pieces.

Continue to check Dropdowndatepicker, find Dojo.provide ("Dojo.widget.DropdownDatePicker"), in its code to find the


if (_e5f== "") {
This.datePicker.setDate ("");
}


This code, and _e5f is changed to the control after the storage string, directly to the


This.datePicker.setDate ("");


Comment out, test it, and find that the nasty Nan will not appear again.

But the date displayed is the old date before the date of the input box, and in normal logic, the input box date is empty, and the current date should be displayed, consistent with the initialization time.

Keep looking, find out.


This.datepicker=dojo.widget.createwidget ("DatePicker", _e5b,this.containernode, "child");


This line of code is a datepicker group piece.

So search DatePicker, find the


Dojo.provide ("Dojo.widget.DatePicker");


Find the Setdate function, find the call to the This._preinitui function, continue to find, finally found in the _preinitui function


if (_e14<this.startdate| | _e14>this.enddate) {
_e14=new Date ((_e14<this.startdate) this.startDate:this.endDate);
}


Only the date size is checked, and there is no "" checksum for the date, and the reason is here. So in


if (_e14<this.startdate| | _e14>this.enddate) {


Before joining


if (_e14== "") {
_e14=new Date ();
}


The revised front and back code is probably as follows:

This.startDate.setHours (0,0,0,0);
This.endDate.setHours (24,0,0,-1);
if (_e14== "") {
_e14=new Date ();
}
if (_e14<this.startdate| | _e14>this.enddate) {
_e14=new Date ((_e14<this.startdate) this.startDate:this.endDate);
}

Test, everything OK, to this perfect solution.


The ultimate Solution

Find The Used
Struts2-dojo-plugin-2.1.8.1.jar
Unzip to the struts2-dojo-plugin-2.1.8.1 folder, and then find
Struts2-dojo-plugin-2.1.8.1/org/apache/struts2/static/dojo
Under the Struts_dojo.js file.
Open File
Found it
Dojo.provide ("Dojo.widget.DatePicker");


and find it down.


if (_e14<this.startdate| | _e14>this.enddate) {
_e14=new Date ((_e14<this.startdate) this.startDate:this.endDate);
}


Add before it


if (_e14== "") {
_e14=new Date ();
}
Save.
Then use WinRAR or WinZip, enter the struts2-dojo-plugin-2.1.8.1 folder, will select all the files under the folder compression, pay attention to the choice of zip format compression, after compression, modified to the. zip to.

OK, now overwrite the corresponding jar package in the project and redeploy it. When testing, pay attention to empty the browser's cache, and then refresh it. Otherwise, it is also the bug when the Struts-dojo.js file is on the client.

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.