Ajax的使用(jquery的下载)

Ajax学习笔记(jquery的下载)

JQuery的官网下载
地址:http://jquery.com

右上角的"Download JQuery"

三个可供下载的文件:
Production JQuery版本:优化压缩后的版本,具有体积小,主要用于部署网站时使用
Development JQuery版本:未压缩版本有266kb的大小,一般在网站建设时使用
JQuery map文件:一般不需要

最好将三个都下载,方便在需要时切换。

几个主要参数

  • URL:请求地址
  • data:发送的数据
  • async:是否异步
  • success:成功之后的回调函数
  • error:失败之后的回调函数

引用jQuery脚本库

<script src="JQuery/jquery-3.3.1.js"></script>

在IDEA中,需要导入静态资源约束。否则找不到js文件。报$不存在

   <!--静态资源过滤器-->
    <mvc:default-servlet-handler/>

1、在网页中引入该js文件
在这里插入图片描述

<script src="${pageContext.request.contextPath}/static/js/jquery-3.6.0.js"></script>

例子ajax的简单使用

页面

<%--
  Created by IntelliJ IDEA.
  User: ff
  Date: 2021/8/1
  Time: 12:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
    <script src="${pageContext.request.contextPath}/static/js/jquery-3.6.0.js"></script>
  </head>
  <body>

  <script type="text/javascript">
    function a(){
      $.post({
        url:"${pageContext.request.contextPath}/ajax/a2",
        data:{"name":$("#name").val()},
        success:function (data){
          alert(data);
        }
      })
    }

  </script>
用户名:<input type="text" id="name" onblur="a()">
  </body>
</html>

controller

package com.zheng.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/ajax")
public class AjaxController {

    @RequestMapping("/a2")
    public void ajax1(String name,HttpServletResponse response) throws IOException {
        if("zheng".equals(name)){
            response.getWriter().println("true");
        }else{
            response.getWriter().println("false");
        }
    }



}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--配置DispatchServlet;这个是springMVC的核心,请求分发器,前端控制器-->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--里边的路径是编写的配置文件-->
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--启动级别-->
        <load-on-startup>1</load-on-startup>
    </servlet>


    <!--在springMVC中,/:代表匹配所有的请求,不包括jsp页面。/*:匹配所有的请求,包括jsp-->
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

springmvc核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!--自动扫描包,让指定包下的注解生效,有ioc容器统一管理-->
    <context:component-scan base-package="com.zheng.controller"/>
    <!--静态资源过滤器-->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>


    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

posted on 2022-08-28 22:20  热爱技术的小郑  阅读(264)  评论(0编辑  收藏  举报