当前位置: 首页 > news >正文

【WEEK4】 【DAY5】AJAX - Part Two【English Version】

2024.3.22 Friday

Following the previous article 【WEEK4】 【DAY4】AJAX - Part One【English Version】

Contents

  • 8.4. Ajax Asynchronous Data Loading
    • 8.4.1. Create User.java
    • 8.4.2. Add lombok and jackson support in pom.xml
    • 8.4.3. Change Tomcat Settings
    • 8.4.4. Modify AjaxController.java
    • 8.4.5. Create test2.jsp
      • 8.4.5.1. Note: At the same level as WEB-INF!
      • 8.4.5.2. Verify Click Event
      • 8.4.5.3. Output the Content of userList
      • 8.4.5.4. Update the JavaScript Version
      • 8.4.5.5. Further Modify test2.jsp to Directly Display userList on the Web Page
    • 8.4.6. Run
  • 8.5. Ajax Username Verification Experience
    • 8.5.1. Modify AjaxController.java
    • 8.5.2. Create login.jsp
    • 8.5.3. Run

8.4. Ajax Asynchronous Data Loading

8.4.1. Create User.java

Insert image description here

package P24.project;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {private String name;private int age;private String gender;
}

8.4.2. Add lombok and jackson support in pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>SpringMVC_try1</artifactId><groupId>com.kuang</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><groupId>P24</groupId><artifactId>springmvc-06-ajax</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.15.2</version></dependency></dependencies></project>

8.4.3. Change Tomcat Settings

Initially, the default value of Application context is /springmvc_06_ajax_war_exploded, it can be changed to / to simplify the URL.
Insert image description here
Insert image description here

8.4.4. Modify AjaxController.java

package P24.controller;import P24.project.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;//Does not return to view resolver
@RestController
public class AjaxController {@RequestMapping("/t1")public String test(){return "hello";}@RequestMapping("/a1")public void a(String name, HttpServletResponse response) throws IOException {System.out.println("a1:param=>"+name);if ("zzz".equals(name)){response.getWriter().print("true");}else {response.getWriter().print("false");}}@RequestMapping("/a2")public List<User> a2(){List<User> userList = new ArrayList<User>();//Adding datauserList.add(new User("zhangsan",11,"male"));userList.add(new User("lisi",22,"female"));userList.add(new User("wangwu",33,"male"));return userList;}
}

8.4.5. Create test2.jsp

8.4.5.1. Note: At the same level as WEB-INF!

Insert image description here
Otherwise, the jsp file cannot be accessed by changing the URL! (As shown below) A 404 error will appear due to incorrect placement, which took almost a day to find, but in reality, it just needs to be correctly positioned at creation!
Insert image description here
If test2.jsp is unintentionally placed under the WEB-INF/jsp directory, it can still be accessed (not recommended), with the following steps:

  1. Modify AjaxController.jsp to use the view resolver approach.
  2. Create method t2 to access test2, at this time, it can be accessed through the view resolver to WEB-INF/jsp/test2.jsp.
package P24.controller;import P24.project.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;@Controller //Will return to view resolver
//@RestController   //Does not return to view resolver//    If you need to access test2.jsp from the controller, you need to use the view resolver in applicationContext.xml
//    In this case, the outermost AjaxController method needs to use the @Controller annotation@GetMapping("/t2")public String t2(){return "/WEB-INF/test2.jsp";}
}
  1. For other methods, refer to the following links for modifications, and be aware of the real save location of the idea project, do not operate on the save location of tomcat (even less recommended, personally think it’s very cumbersome)
    https://www.cnblogs.com/jet-angle/p/11477297.html
    https://www.cnblogs.com/atsong/p/13118155.html

8.4.5.2. Verify Click Event

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title><script src="${pageContext.request.contextPath}/statics/js/jquery-3.7.1.js"></script><script>$(function () {$("#btn").click(function (){console.log("test2");})});</script>
</head><body>
<%--First capture the event--%>
<input type="button" value="Load Data" id="btn">
<%--Display with a table--%>
<table><tr><td>Name</td><td>Age</td><td>Gender</td></tr><tbody><%--Data is in the backend, cannot be directly fetched, thus requires a “request”--%></tbody>
</table>
</body>
</html>

http://localhost:8080/springmvc_06_ajax_war_exploded/test2.jsp
Press F12 to open the console page, then click “Load Data” to see console.log("test2"); run, outputting “test2”
Insert image description here

8.4.5.3. Output the Content of userList

Simply modify the function from the previous step

  <script>$(function () {$("#btn").click(function (){//$.post(url, param[optional], success)$.post("${pageContext.request.contextPath}/a2",function(data){console.log(data);})})});</script>

http://localhost:8080/springmvc_06_ajax_war_exploded/test2.jsp
Insert image description here

8.4.5.4. Update the JavaScript Version

As shown, the default is generally 6+
Insert image description here
Insert image description here

8.4.5.5. Further Modify test2.jsp to Directly Display userList on the Web Page

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title><script src="${pageContext.request.contextPath}/statics/js/jquery-3.7.1.js"></script><script>$(function () {$("#btn").click(function (){//$.post(url, param[optional], success)$.post("${pageContext.request.contextPath}/a2",function(data){// console.log(data);var html="";for (let i = 0; i < data.length; i++) {html += "<tr>" +"<td>" + data[i].name + "</td>" +"<td>" + data[i].age + "</td>" +"<td>" + data[i].sex + "</td>" +"</tr>"}$("#content").html(html); //Put the elements from var html="<>"; into this line});})});</script>
</head><body>
<%--First capture the event--%>
<input type="button" value="Load Data" id="btn">
<%--Display with a table--%>
<table><tr><td>Name</td><td>Age</td><td>Gender</td></tr><tbody id="content"><%--Data is in the backend, cannot be directly fetched, thus requires a “request”--%></tbody>
</table>
</body>
</html>

8.4.6. Run

http://localhost:8080/springmvc_06_ajax_war_exploded/test2.jsp
Insert image description here

8.5. Ajax Username Verification Experience

8.5.1. Modify AjaxController.java

package P24.controller;import P24.project.User;
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.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;//@Controller //Returns to the view resolver
@RestController   //Does not return to the view resolver
public class AjaxController {@RequestMapping("/t1")public String test(){return "hello";}//    If access to test2.jsp is needed from the controller, the view resolver in applicationContext.xml must be used
//    In this case, the outermost AjaxController method needs to use the @Controller annotation
//    @GetMapping("/t2")
//    public String t2(){
//        return "/WEB-INF/test2.jsp";
//    }@RequestMapping("/a1")public void a(String name, HttpServletResponse response) throws IOException {System.out.println("a1:param=>" + name);if ("zzz".equals(name)){response.getWriter().print("true");}else {response.getWriter().print("false");}}@RequestMapping("/a2")public List<User> a2(){List<User> userList = new ArrayList<User>();//Add datauserList.add(new User("zhangsan", 11, "male"));userList.add(new User("lisi", 22, "female"));userList.add(new User("wangwu", 33, "male"));return userList;}@RequestMapping("/a3")public String a3(String name, String pwd){String msg = "";if (name != null){//admin, these data should be in the databaseif ("admin".equals(name)){msg = "Accepted";}else {msg = "name has an error";}}if (pwd != null){//123456, these data should be in the databaseif ("123456".equals(pwd)){msg = "Accepted";}else {msg = "password has an error";}}return msg;}
}

8.5.2. Create login.jsp

Insert image description here

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title><script src="${pageContext.request.contextPath}/statics/js/jquery-3.7.1.js"></script><script>function a1(){$.post({url:"${pageContext.request.contextPath}/a3",data:{'name':$("#name").val()},success:function (data) {if (data.toString() == 'Accepted'){$("#userInfo").css("color","green");}else {$("#userInfo").css("color","red");}$("#userInfo").html(data);  //Print data on the webpage}});}function a2() {$.post({url:"${pageContext.request.contextPath}/a3",data:{'pwd':$("#pwd").val()},success:function (data) {if (data.toString() == 'Accepted'){$("#pwdInfo").css("color","green");}else {$("#pwdInfo").css("color","red");}$("#pwdInfo").html(data);}});}</script>
</head>
<body><p>
<%--  On focus loss--%>Username: <input type="text" id="name" onblur="a1()">
<%--  Prompt information--%><span id="userInfo"></span>
</p>
<p>Password: <input type="text" id="pwd" onblur="a2()"><span id="pwdInfo"></span>
</p></body>
</html>

8.5.3. Run

http://localhost:8080/springmvc_06_ajax_war_exploded/login.jsp
Insert image description here
Insert image description here

相关文章:

【WEEK4】 【DAY5】AJAX - Part Two【English Version】

2024.3.22 Friday Following the previous article 【WEEK4】 【DAY4】AJAX - Part One【English Version】 Contents 8.4. Ajax Asynchronous Data Loading8.4.1. Create User.java8.4.2. Add lombok and jackson support in pom.xml8.4.3. Change Tomcat Settings8.4.4. Mo…...

力扣100热题[哈希]:最长连续序列

原题&#xff1a;128. 最长连续序列 题解&#xff1a; 官方题解&#xff1a;. - 力扣&#xff08;LeetCode&#xff09;题解&#xff0c;最长连续序列 &#xff1a;哈希表 官方解题思路是先去重&#xff0c;然后判断模板长度的数值是否存在&#xff0c;存在就刷新&#xff0c…...

python笔记基础--文件和存储数据(7)

目录 1.从文件中读取数据 2.写入文件 3.存储数据 3.1使用json.dump()和json.load() 3.2保存和读取用户生成的数据 3.3重构 1.从文件中读取数据 读取整个文件 with open(data.txt) as file_object: contents file_object.read()print(contents)print(contents.rstrip…...

Vue黑马笔记(最新)

VUE vue是一个用于构建用户界面的渐进式框架 创建一个VUE实例 核心步骤&#xff1a; 准备容器引包&#xff08;官网&#xff09;-开发版本/生产版本创建一个vue实例 new vue()指定配置项->渲染数据 el指定挂载点&#xff08;选择器&#xff09;,指定管理的是哪个容器。dat…...

安全工具介绍 SCNR/Arachni

关于SCNR 原来叫Arachni 是开源的&#xff0c;现在是SCNR&#xff0c;商用工具了 可试用一个月 Arachni Web Application Security Scanner Framework 看名字就知道了&#xff0c;针对web app 的安全工具&#xff0c;DASTIAST吧 安装 安装之前先 sudo apt-get update sudo…...

赋能数据收集:从机票网站提取特价优惠的JavaScript技巧

背景介绍 在这个信息时代&#xff0c;数据的收集和分析对于旅游行业至关重要。在竞争激烈的市场中&#xff0c;实时获取最新的机票特价信息能够为旅行者和旅游企业带来巨大的优势。 随着机票价格的频繁波动&#xff0c;以及航空公司和旅行网站不断推出的限时特价优惠&#xff…...

【大模型】在VS Code(Visual Studio Code)上安装中文汉化版插件

文章目录 一、下载安装二、配置显示语言&#xff08;一&#xff09;调出即将输入命令的搜索模式&#xff08;二&#xff09;在大于号后面输入&#xff1a;Configure Display Language&#xff08;三&#xff09;重启 三、总结 【运行系统】win 11 【本文解决的问题】 1、英文不…...

自定义WordPress顶部的菜单的方法

要自定义WordPress顶部的菜单&#xff0c;你需要使用WordPress的菜单系统。首先&#xff0c;你需要创建自定义菜单&#xff0c;然后将其设置为顶部导航菜单。 以下是创建自定义菜单并设置其为顶部导航菜单的步骤&#xff1a; 登录到WordPress管理界面。转到“外观”>“菜单…...

独孤思维:流量暴涨,却惨遭违规

最近独孤操作虚拟资料短视频&#xff0c;有个很深的感悟。 每天发10条短视频&#xff0c;积累到20天左右&#xff0c;播放量和粉丝数开始暴涨。 虽然很多牛比的比我数据好&#xff0c;但是对于刚做短视频的独孤来说&#xff0c;我已经满足了。 但是又发了10来天&#xff0c;…...

【python 装饰器 - 重试】做一个简易重试装饰器,如果函数执行错误则会自动重新执行,可设置重试次数,对爬虫比较友好

文章日期&#xff1a;2024.03.19 使用工具&#xff1a;Python 类型&#xff1a;装饰器 文章全程已做去敏处理&#xff01;&#xff01;&#xff01; 【需要做的可联系我】 AES解密处理&#xff08;直接解密即可&#xff09;&#xff08;crypto-js.js 标准算法&#xff09;&…...

Linux线程补充之——同步

一、Linux线程同步 ​ 同步是相对于竞争的概念&#xff1b; ​ 同步就是在保证安全的前提下啊&#xff0c;按照一定的顺序访问临界资源&#xff1b; ​ 所有的资源一定是先访问的临界资源&#xff0c;申请失败然后才进行排队的&#xff1b;互斥锁保证的是来访问的进程只允许…...

面试九 设计模式

单例模式通常被归类为创建型设计模式&#xff0c;因为它主要关注如何创建对象的实例&#xff0c;以及如何确保在整个应用程序生命周期中只有一个实例存在。 1.为什么日志模块和数据库连接池需要单例模式 使用单例模式来实现数据库连接池主要有以下几个原因&#xff1a; 全局唯…...

c++和c语言的区别实例

C和C语言在程序设计领域内具有深远的影响&#xff0c;它们不仅丰富了编程的世界&#xff0c;也为软件开发人员提供了强大的工具。虽然C是在C语言的基础上发展起来的&#xff0c;但两者之间存在着一些关键的区别。为了更深入地理解这些不同&#xff0c;本文将从多个维度探讨C和C…...

图论基础|841.钥匙和房间、463. 岛屿的周长

目录 841.钥匙和房间 思路&#xff1a;本题是一个有向图搜索全路径的问题。 只能用深搜&#xff08;DFS&#xff09;或者广搜&#xff08;BFS&#xff09;来搜。 463. 岛屿的周长 841.钥匙和房间 力扣题目链接 (opens new window) 有 N 个房间&#xff0c;开始时你位于 0…...

把 Taro 项目作为一个完整分包,Taro项目里分包的样式丢失

现象&#xff1a; 当我们把 Taro 项目作为原生微信小程序一个完整分包时&#xff0c;Taro项目里分包的样式丢失&#xff0c;示意图如下&#xff1a; 原因&#xff1a; 在node_modules/tarojs/plugin-indie/dist/index.js文件里&#xff0c;限制了只有pages目录下会被引入app.w…...

腾讯云服务器价格查询系统,2024年1年、3年和5年活动价格表

腾讯云服务器多少钱一年&#xff1f;61元一年起。2024年最新腾讯云服务器优惠价格表&#xff0c;腾讯云轻量2核2G3M服务器61元一年、2核2G4M服务器99元一年可买三年、2核4G5M服务器165元一年、3年756元、轻量4核8M12M服务器646元15个月、4核16G10M配置32元1个月、312元一年、8核…...

第十四届蓝桥杯大赛软件赛省赛Java大学B组

最近正在备考蓝桥杯&#xff0c;报的java b组&#xff0c;顺便更一下蓝桥的 幸运数字 题目 思路&#xff1a;填空题&#xff0c;暴力即可 import java.util.Scanner; // 1:无需package // 2: 类名必须Main, 不可修改public class Main {static int trans(int x, int y){int …...

Java二阶知识点总结(七)SVN和Git

SVN 1、SVN和Git的区别 SVN是集中式的&#xff0c;也就是会有一个服务器保存所有代码&#xff0c;拉取代码的时候只能从这个服务器上拉取&#xff1b;Git是分布式的&#xff0c;也就是说每个人都保存有所有代码&#xff0c;如果要获取代码&#xff0c;可以从其他人手上获取SV…...

Java后端八股------设计模式

Coffee可以设计成接口。...

DBO优化GRNN回归预测(matlab代码)

DBO-GRNN回归预测matlab代码 蜣螂优化算法(Dung Beetle Optimizer, DBO)是一种新型的群智能优化算法&#xff0c;在2022年底提出&#xff0c;主要是受蜣螂的的滚球、跳舞、觅食、偷窃和繁殖行为的启发。 数据为Excel股票预测数据。 数据集划分为训练集、验证集、测试集,比例…...

App无辜躺枪?手把手教你搞定腾讯手机管家误报导致的应用商店下架

当合规应用遭遇误报下架&#xff1a;开发者系统性应对指南运动健康类应用被标记为金融诈骗软件&#xff1f;社交工具因"病毒风险"被各大商店紧急下架&#xff1f;这类看似荒谬的误报事件&#xff0c;正在成为中小开发团队的"无妄之灾"。某知名运动App开发团…...

保姆级教程:在ArcGIS Pro插件中集成你的自定义工具箱(以‘消除重复要素’为例)

从脚本到按钮&#xff1a;ArcGIS Pro插件开发实战指南 在GIS日常工作中&#xff0c;我们常常会遇到一些重复性的数据处理任务。比如数据质检环节的"消除重复要素"操作&#xff0c;虽然可以通过Python脚本实现&#xff0c;但每次都需要打开IDE或Python窗口执行代码&am…...

DIY复刻经典:Texar Audio Prism动态处理器克隆套件全攻略

1. 项目概述&#xff1a;Texar Audio Prism 克隆套件如果你在专业音频圈子里混过一段时间&#xff0c;尤其是对上世纪八九十年代那些经典的、带点“魔法”色彩的外置动态处理器感兴趣&#xff0c;那么“Texar Audio Prism”这个名字你大概率不会陌生。它不是最常见的1176或者LA…...

新手村任务:成为一个架构师需要哪些装备?

新手村任务:成为一个架构师需要哪些装备? 一、前言 如果你刚入行不久,想成为一名架构师,那这篇文章就是为你写的。 我们把成为架构师比作一个RPG游戏,你是主角,需要收集各种装备、刷经验、升级技能。 新手村的第一个任务就是:了解你需要哪些装备。 二、架构师技能树…...

适合地产人用的中介房源管理系统

在房产经纪行业&#xff0c;房源管理与客源管理是经纪人日常工作的核心&#xff0c;直接影响业务效率与成交转化。选择一套适配行业需求的中介房源管理系统&#xff0c;能帮助中介团队规范流程、降低运营成本、大幅提升业绩。今天我们以客观视角&#xff0c;详细解析全房源系统…...

2026年,本地精准营销高性价比服务商来袭,你还不了解一下?

在本地商业竞争日益激烈的2026年&#xff0c;实体店面临着诸多挑战&#xff0c;引流难、成本高、复购率低等问题困扰着众多商家。而中粤&#xff08;广州&#xff09;信息科技有限公司作为本地精准营销的高性价比服务商&#xff0c;正以其独特的优势和卓越的服务&#xff0c;为…...

对比 Token Plan 与按量计费在 Taotoken 平台上的成本体感差异

&#x1f680; 告别海外账号与网络限制&#xff01;稳定直连全球优质大模型&#xff0c;限时半价接入中。 &#x1f449; 点击领取海量免费额度 对比 Token Plan 与按量计费在 Taotoken 平台上的成本体感差异 对于个人开发者或项目管理者而言&#xff0c;在接入大模型服务时&a…...

嵌入式快速原型开发:基于Sceptre平台与LPC2148的实战指南

1. 项目概述&#xff1a;Sceptre&#xff0c;一个被低估的嵌入式快速原型利器 在嵌入式开发的世界里&#xff0c;我们总是在寻找那个“刚刚好”的平台&#xff1a;它要足够强大&#xff0c;能跑复杂的算法&#xff1b;要足够小巧&#xff0c;能塞进各种外壳&#xff1b;要足够便…...

网络配置工具类详解

CNet 网络配置工具类详解平台&#xff1a;仅支持 Linux&#xff0c;大量使用 ioctl 系统调用一、概述 CNet 是一个 纯静态方法的网络配置工具类&#xff0c;封装了 Linux 下常用的网络操作&#xff1a;功能类别涵盖内容IP 地址读取/设置本机 IP、子网掩码网关读取/添加/删除/设…...

厨房空调技术白皮书:从风冷到水冷,制冷系统在厨房场景中的工程化演进

厨房空调是暖通行业近三年技术迭代最密集的细分品类。从最初的"凉霸"&#xff08;本质是风扇&#xff09;&#xff0c;到风冷分体式&#xff0c;再到水冷一体式&#xff0c;每代技术都在解决上一代没有覆盖的用户痛点。本文以工程技术视角&#xff0c;梳理四代厨房制…...