Jsp page:
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">
<Script type = "text/javascript" src = "http://code.jquery.com/jquery-1.10.1.min.js"> </script>
<Script type = "text/javascript" src = "http://code.jquery.com/jquery-migrate-1.2.1.min.js"> </script>
<Script type = "text/javascript" src = "<% = basePath %>/js/timeago. js"> </script>
</Head>
<Body>
This is my JSP page. <br>
<P >$ {time} </p>
<Span class = "time timeago" title = "2014-4-29 15:23"> </span>
</Body>
</Html>
<Script type = "text/javascript">
JQuery ("span. timeago"). timeago ();
</Script>
Copy codeThe Code is as follows:
(Function (factory ){
If (typeof define ==== 'function' & define. amd ){
// AMD. Register as an anonymous module.
Define (['jquery '], factory );
} Else {
// Browser globals
Factory (jQuery );
}
} (Function ($ ){
$. Timeago = function (timestamp ){
If (timestamp instanceof Date ){
Return inWords (timestamp );
} Else if (typeof timestamp = "string "){
Return inWords ($. timeago. parse (timestamp ));
} Else if (typeof timestamp = "number "){
Return inWords (new Date (timestamp ));
} Else {
Return inWords ($. timeago. datetime (timestamp ));
}
};
Var $ t = $. timeago;
$. Extend ($. timeago ,{
Settings :{
RefreshMillis: 60000,
AllowFuture: false,
LocaleTitle: false,
Cutoff: 0,
Strings :{
PrefixAgo: null,
PrefixFromNow: null,
SuffixAgo: "",
SuffixFromNow: "from now ",
Seconds: "1 minute ",
Minute: "1 minute ",
Minutes: "% d minutes ",
Hour: "1 hour ",
Hours: "% d Hour ",
Day: "1 day ",
Days: "% d days ",
Month: "January ",
Months: "% d month ",
Year: "1 year ",
Years: "% d ",
WordSeparator :"",
Numbers: []
}
},
InWords: function (distanceMillis ){
Var $ l = this. settings. strings;
Var prefix = $ l. prefixAgo;
Var suffix = $ l. suffixAgo;
If (this. settings. allowFuture ){
If (distanceMillis <0 ){
Prefix = $ l. prefixFromNow;
Suffix = $ l. suffixFromNow;
}
}
Var secondds = Math. abs (distanceMillis)/1000;
Var minutes = seconds/60;
Var hours = minutes/60;
Var days = hours/24;
Var years = days/365;
Function substitute (stringOrFunction, number ){
Var string = $. isFunction (stringOrFunction )? StringOrFunction (number, distanceMillis): stringOrFunction;
Var value = ($ l. numbers & $ l. numbers [number]) | number;
Return string. replace (/% d/I, value );
}
Var words = seconds <45 & substitute ($ l. seconds, Math. round (seconds) |
Seconds <90 & substitute ($ l. minute, 1) |
Minutes <45 & substitute ($ l. minutes, Math. round (minutes) |
Minutes <90 & substitute ($ l. hour, 1) |
Hours <24 & substitute ($ l. hours, Math. round (hours) |
Hours <42 & substitute ($ l. day, 1) |
Days <30 & substitute ($ l. days, Math. round (days) |
Days <45 & substitute ($ l. month, 1) |
Days <365 & substitute ($ l. months, Math. round (days/30) |
Years <1.5 & substitute ($ l. year, 1) |
Substitute ($ l. years, Math. round (years ));
Var separator = $ l. wordSeparator | "";
If ($ l. wordSeparator === undefined) {separator = "";}
Return $. trim ([prefix, words, suffix]. join (separator ));
},
Parse: function (iso8601 ){
Var s = $. trim (iso8601 );
S = s. replace (/\. \ d +/, ""); // remove milliseconds
S = s. replace (/-/, "/"). replace (/-/,"/");
S = s. replace (/T/, ""). replace (/Z/, "UTC ");
S = s. replace (/([\ + \-] \ d )\:? (\ D)/, "$1 $2"); //-->-0400
Return new Date (s );
},
Datetime: function (elem ){
Var iso8601 = $ t. isTime (elem )? $ (Elem). attr ("datetime"): $ (elem). attr ("title ");
Return $ t. parse (iso8601 );
},
IsTime: function (elem ){
// JQuery's is () 'doesn' t play well with HTML5 in IE
Return $ (elem). get (0). tagName. toLowerCase () = "time"; // $ (elem). is ("time ");
}
});
// Functions that can be called via $ (el). timeago ('action ')
// Init is default when no action is given
// Functions are called with context of a single element
Var functions = {
Init: function (){
Var refresh_el = $. proxy (refresh, this );
Refresh_el ();
Var $ s = $ t. settings;
If ($ s. refreshMillis> 0 ){
SetInterval (refresh_el, $ s. refreshMillis );
}
},
Update: function (time ){
$ (This). data ('timeago ', {datetime: $ t. parse (time )});
Refresh. apply (this );
},
UpdateFromDOM: function (){
$ (This). data ('timeago ', {datetime: $ t. parse ($ t. isTime (this )? $ (This). attr ("datetime"): $ (this). attr ("title "))});
Refresh. apply (this );
}
};
$. Fn. timeago = function (action, options ){
Var fn = action? Functions [action]: functions. init;
If (! Fn ){
Throw new Error ("Unknown function name'" + action + "'for timeago ");
}
// Each over objects here and call the requested function
This. each (function (){
Fn. call (this, options );
});
Return this;
};
Function refresh (){
Var data = prepareData (this );
Var $ s = $ t. settings;
If (! IsNaN (data. datetime )){
If ($ s. cutoff = 0 | distance (data. datetime) <$ s. cutoff ){
$ (This). text (inWords (data. datetime ));
}
}
Return this;
}
Function prepareData (element ){
Element = $ (element );
If (! Element. data ("timeago ")){
Element. data ("timeago", {datetime: $ t. datetime (element )});
Var text = $. trim (element. text ());
If ($ t. settings. localeTitle ){
Element. attr ("title", element. data ('timeago '). datetime. toLocaleString ());
} Else if (text. length> 0 &&! ($ T. isTime (element) & element. attr ("title "))){
Element. attr ("title", text );
}
}
Return element. data ("timeago ");
}
Function inWords (date ){
Return $ t. inWords (distance (date ));
}
Function distance (date ){
Return (new Date (). getTime ()-date. getTime ());
}
// Fix for IE6 suckage
Document. createElement ("abbr ");
Document. createElement ("time ");
}));
Controller layer:
Copy codeThe Code is as follows:
Package com. spring. controller;
Import java. io. IOException;
Import java. io. PrintWriter;
Import java. util. Date;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import net. sf. json. JSONArray;
Import org. springframework. stereotype. Controller;
Import org. springframework. web. bind. annotation. RequestMapping;
Import org. springframework. web. bind. annotation. RequestMethod;
Import org. springframework. web. servlet. ModelAndView;
Import com. spring. model. JsonMoel;
Import com.sun.org. apache. bcel. internal. generic. NEW;
/**
* @ Author Qixuan. Chen
* Creation Time:
*/
@ Controller
Public class timeago controller {
/**
* @ Param request
* @ Param response
* @ Return
* @ Throws IOException
*/
@ RequestMapping (value = "time/show", method = {RequestMethod. POST, RequestMethod. GET })
Public ModelAndView PostJsonTest (HttpServletRequest request, HttpServletResponse response) throws IOException {
ModelAndView mav = new ModelAndView ();
Mav. addObject ("time", new Date ());
Mav. setViewName ("time/timeago ");
Return mav;
}
}