Java后端开发——Ajax、jQuery和JSON

概述

Ajax全称是Asynchronous Javascript and XML,即异步的JavaScript和 XML。Ajax是一种Web应用技术,该技术是在JavaScript、DOM、服务器配合下,实现浏览器向服务器发送异步请求。

Ajax异步请求方式不向服务器发出请求,会得到数据后再更新页面(通过DOM操作修改页面内容),整个过程不会发生页面跳转或刷新操作。 传统请求方式和Ajax异步请求方式区别

Ajax九九乘法口诀表

1.创建页面mul99ajax.html,添加num、button和result标记

乘法口诀表

阶数:

2.添加Ajax的JS代码

1)DOM查找元素num和result标记

var num = document.getElementById("num").value;

var result = document.getElementById("result");

2)创建XHR对象

var xhr = new XMLHttpRequest();

3)发送请求

xhr.open('get', '/myspringmvc/calAjax?num='+num);

xhr.send();

4)设置回调函数

xhr.onreadystatechange = function () {

if (xhr.readyState === 4 && xhr.status === 200) {

result.innerHTML =xhr.responseText;

}

}

5)处理异步数据,更新数据到result标记

result.innerHTML =xhr.responseText;

3.编写后端SpringMVC代码MultableController.java,在处理方法上增加注解@ResponseBody,返回值为内容。

@Controller

public class MultableController {

@RequestMapping("/calAjax")

@ResponseBody // 返回内容,不是JSP页面

public String cal(Integer num) {

Multable m=new Multable();

m.setNum(num);

String result=m.print();

return result;

}

}

jQuery实现表格奇偶行颜色不同 1.创建页面jQuery.html,添加表格标记和测试数据

姓名性别暂住地
张三四川成都
李四四川绵阳
王五四川南充
赵六四川自贡
陈勇四川德阳
罗梅四川宜宾

2.设置CSS样式,奇偶行颜色不同

3.编写jQuery代码设置奇偶行颜色不同,设置click改变颜色

Insert title here

姓名性别暂住地
张三四川成都
李四四川绵阳
王五四川南充
赵六四川自贡
陈勇四川德阳
罗梅四川宜宾

jQuery版九九乘法口诀

1.在创建页面mul99jquery.html,添加num、button和result标记

Insert title here

乘法口诀表

阶数:

2.添加jQuery的Ajax的JS代码

1)在jQuery页面函数中,给按钮添加事件代码

$(document).ready(function(){

$('#btn').click(function(){

var num=$('#num').val();

$.ajax({

url: '/myspringmvc/calAjax',

type: 'post',

data:{num:num},

success: function(data){

$('#result').html(data);

}

})

})

})

2)获取num控件的值 var num=KaTeX parse error: Expected 'EOF', got '#' at position 3: ('#̲num').val(); 3)….ajax()函数,设置url、type和success函数。

$.ajax({

url: '/myspringmvc/calAjax',

type: 'post',

data:{num:num},

success: function(data){

$('#result').html(data);

}

3.测试 测试结果 商品类型Ajax加载 1.创建type表,并录入测试的商品类型数据

CREATE TABLE `type` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(45) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb4;

插入数据

INSERT INTO `type` VALUES ('1', '零食');

INSERT INTO `type` VALUES ('2', '饮料');

INSERT INTO `type` VALUES ('3', '香烟');

INSERT INTO `type` VALUES ('7', '文具');

INSERT INTO `type` VALUES ('8', '猕猴桃');

INSERT INTO `type` VALUES ('10', '蛋糕');

INSERT INTO `type` VALUES ('11', '哈皮');

INSERT INTO `type` VALUES ('12', '芒果');

INSERT INTO `type` VALUES ('15', '果子');

INSERT INTO `type` VALUES ('16', '果子');

INSERT INTO `type` VALUES ('17', '果子');

刷新之后,如图所示 2.添加Javabean类Type.java 和 Dao类TypeDao.java

package com.javaweb.bean;

public class Type {

private Integer id;

private String name;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

package com.javaweb.controller;

import java.sql.SQLException;

import java.util.List;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.javaweb.bean.Type;

import com.javaweb.dao.TypeDao;

@Controller

@RequestMapping("/type")

public class TypeController {

private TypeDao typeDao=new TypeDao();

@GetMapping("/find")

@ResponseBody

public List find(){

try {

return typeDao.find();

} catch (SQLException e) {

e.printStackTrace();

}

return null;

}

}

3.修改good_add.jsp,添加jQuery代码,渲染类型列表

Insert title here

src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.0.0/jquery.min.js">

4.预览商品添加表单,查看类型列表 5.修改good_edit.jsp,添加jQuery代码,渲染类型列表选中

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

Insert title here

src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.0.0/jquery.min.js">

修改商品页面



推荐尺寸: 500 * 500

推荐尺寸: 500 * 500

推荐尺寸: 500 * 500

6.预览商品编辑表单,查看类型列表及选中

上面项目已打包上传到网盘,需要可以自取。附链接:https://pan.baidu.com/s/1HmVI00L_C7Zx3bqTEqzXnA?pwd=2024

后面有时间和精力也会分享更多关于大数据领域方面的优质内容,感谢各位的喜欢与支持!

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: