全屏浏览
缩小浏览
回到页首

日记整理---->2016-11-23

  这里放一些jquery的学习知识。可能从一开始就是我一个人单枪匹马,来年不求并肩作战,只愿所向披靡。

jquery的学习一

jquery关于ajax的一些学习博客

ajax方法的介绍:https://learn.jquery.com/ajax/jquery-ajax-methods/

ajax方法jsonp:https://learn.jquery.com/ajax/working-with-jsonp/

jquery中deferreds:https://learn.jquery.com/code-organization/deferreds/

jquery的api学习:https://api.jquery.com/

 

jquery关于ajax的代码示例

一、我们通过一个案例来学习ajax的一些细节

  • html的代码:
<input type="text" id="username">&nbsp;&nbsp;
<button onclick="ajaxTest()">click me</button>
  •  js的代码:
function ajaxTest() {
    var username = $("#username").val();
    var data = {
        username: username
    }
    $.ajax({
        url: "loginAction/userLogin.action",
        data: data,
        type: "GET"
    }).done(function(response, textStatus, jqXHR ) {
        console.log("done methods." + response);
        console.log("textStatus " + textStatus);
    }).fail(function( xhr, status, errorThrown ) {
        console.log( "Error: " + errorThrown );
        console.log( "Status: " + status );
        console.dir( xhr );
    }).always(function( xhr, status ) {
        console.log( "The request is complete!" );
    });
}
  • java的代码:
@Controller
@RequestMapping(value = "loginAction")
public class LoginAction {
    @RequestMapping(value = "userLogin")
    public void userLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, Exception {
        String username = (String) request.getAttribute("username");
        if (StringUtils.isEmpty(username) ) {
            throw new RuntimeException();
        } else if (username.equals("huhx")) {
            request.getRequestDispatcher("/html/fail.html").forward(request, response);
        } else if (username.equals("linux")){
            response.sendRedirect("/BaseTest/html/redicect.html");
        } else {
            response.getWriter().write("Hello World");
        }
    }
}

二、我们看一下demo运行的效果:

1、当username为空时,也就是用户没有输入: 顺序执行了fail方法和always方法,后台报错。

2、当username为huhx时:顺序执行了done方法和always方法,done里面的response内容是fail.html的内容。

3、当username为linux时,顺序执行了done方法和always方法,done里面的response内容是redicect.html的内容。

4、当username为其它时,顺序执行了done方法和always方法,done里面的response内容是字符串Hello World。

三、关于上述几个方法,官方的说明:

  • jqXHR.done(function(data, textStatus, jqXHR) {});

  An alternative construct to the success callback option, refer to deferred.done() for implementation details. 200和302都会执行done方法。

  •  jqXHR.fail(function(jqXHR, textStatus, errorThrown) {});

  An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.

  • jqXHR.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) { }); (added in jQuery 1.6) 

  An alternative construct to the complete callback option, the .always() method replaces the deprecated .complete() method.In response to a successful request, the function's   arguments are the same as those of .done(): data, textStatus, and the jqXHR object. For failed requests the arguments are the same as those of .fail(): the jqXHR object, textStatus, and errorThrown. Refer to deferred.always() for implementation details.

  • jqXHR.then(function(data, textStatus, jqXHR) {}, function( jqXHR, textStatus, errorThrown ) {});

  Incorporates the functionality of the .done() and .fail() methods, allowing (as of jQuery 1.8) the underlying Promise to be manipulated. Refer to deferred.then() for implementation details. 

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

 

dom元素选择器

一、文档中id只能是唯一的,class可以多个存在。

<div id="huhxDiv" class="huhxclass">
    <span class="huhxclass">Hello World</span>
</div>
<div id="huhxDiv" class="huhxclass">
    <span>Hello World</span>
</div>

通知js获得的结果如下:

console.log($("#huhxDiv").length); // 1,只能得到第一个找到的匹配元素
console.log($(".huhxclass").length); // 3
console.log(document.getElementById("huhxDiv").length); // undefined,由于原生的div是没有length属性的

 

java中的一些小知识

一、String类的compare方法

String str1 = "Abc", str2 = "ACc";
System.out.println(str1.compareTo(str2));

 具体的源代码如下:

public int compareTo(String anotherString) {
    int len1 = value.length;
    int len2 = anotherString.value.length;
    int lim = Math.min(len1, len2);
    char v1[] = value;
    char v2[] = anotherString.value;

    int k = 0;
    while (k < lim) {
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
    }
    return len1 - len2;
}

 

 二、关于Date类的一些用法

Date date1 = new Date(2015 - 1900, 1, 3, 13, 23, 45); // 2015年2月
Date now = new Date();
System.out.println(now.getTime() - date1.getTime());
System.out.println(now.compareTo(date1)); // 时间先后的比较-1
System.out.println(date1);

 运行的效果如下:注意new Date()的第一个参数,需要减去1900。对于js,不需要减去1900的。

61963306022
1
Tue Feb 03 13:23:45 CST 2015

关于Date类里面的compareTo方法,源代码如下:

public int compareTo(Date anotherDate) {
    long thisTime = getMillisOf(this);
    long anotherTime = getMillisOf(anotherDate);
    return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
}

 

三、关于UUID的一些用法

UUID uuid = UUID.randomUUID();
String uuidStr = uuid.toString().replaceAll("-", "");
System.out.println(uuid.toString());
System.out.println(uuidStr);

运行的效果如下:

dec86160-d271-439b-8825-4046bba2ad94
dec86160d271439b88254046bba2ad94

 

四、Calendar类的一些使用

按理说new Date(int, int, int)等方法已经废弃了,官方推荐使用的是Calendar。基础使用方法如下:

Calendar calendar = Calendar.getInstance();
calendar.set(2015, 1, 3, 13, 23, 34); // 这个和上面的时间设置是一样的
System.out.println(calendar.getTime().toString());

运行的效果为: Tue Feb 03 13:23:34 CST 2015

Calendar中提供了before和after方法来比较时间的先后,关于before方法的代码如下:

public boolean before(Object when) {
    return when instanceof Calendar && compareTo((Calendar)when) < 0;
}

 

五、TimeUnit的使用

TimeUnit.MILLISECONDS.sleep(1000);

 

友情链接

 

posted @ 2017-07-28 23:06  huhx  阅读(186)  评论(0编辑  收藏  举报