页面数据类型为json,后端接受json数据

项目结构

在这里插入图片描述

依赖pom.xml

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">

<!--  配置前端控制器-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

<!--  编码过滤器-->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>


addStudent.jsp

取得input 的输入值然后编写json数据,JSON.stringify(student) 将student 转化为json对象


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="/JQ/jquery.js"></script>
    <script>
        $(function (){
            $(":button").click(function (){
                let student = {
                    "id":$("[name=id]:text").val(),
                    "name":$("[name=name]:text").val(),
                    "age":$("[name=age]:text").val(),
                    "subject":$("[name=subject]:text").val(),
                    "address":$("[name=address]:text").val()
                };
                $.ajax({
                    "url":"http://localhost:8080/testAjax",
                    "type":"POST",
                    "dataType":"json",
                    "contentType":"application/json",
                    "data":JSON.stringify(student),
                    "success":function (data){
                        console.log(data)
                    }
                })
            })
        })
    </script>
</head>
<body>
<div>
    <input type="text" name="id" placeholder="ID"><br>
    <input type="text" name="name" placeholder="name"><br>
    <input type="text" name="age" placeholder="age"><br>
    <input type="text" name="subject" placeholder="subject"><br>
    <input type="text" name="address" placeholder="address"><br>
    <input type="button" value="添加"><br>
</div>
</body>
</html>

Student实体类,get set toString方法

在这里插入图片描述

StudentController代码

package com.tmg.controller;

import com.tmg.domain.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class StudentController {

    List<Student> students=new ArrayList<>();
    @ResponseBody
    @RequestMapping("testAjax")
    public List<Student> testAjax(@RequestBody Student student){
        students.add(student);
        return students;
    }

    @RequestMapping("hello")
    public String toIndex(){
        System.out.println("hello");
        return "addStudent";
    }
}

resources下spring.xml

<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.tmg"></context:component-scan>

<!--    配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

<!--   配置静态资源的处理器 -->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

<!--    配置注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>


</beans>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/331673.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

ES可视化工具--ElasticHD

说明 ElasticHD 是 github 上的一个开源的项目&#xff0c;所以他没有官方网站&#xff0c;但 github 上的项目界面也可称为是它的官方界面了。 在 github 上直接搜索 ElasticHD 即可找到它&#xff0c;下面我将留下它的直接跳转链接。ElasticHD 下载 在 github 上搜索到之后…

【MCAL】ADC模块详解

目录 前言 正文 1.ADC模块介绍 2.关键概念及依赖的模块 2.1 ADC依赖的模块 3.ADC功能示例 3.1 ADC Buffer Access Mode示例 3.1.1配置&#xff08;Configuration&#xff09; 3.1.2 初始化&#xff08;Initialization&#xff09; 3.1.3 Adc_GetStreamLastPointer的使…

漏洞检测和评估【网站子域扫描工具02】

上一篇&#xff1a;爬取目标网站的域名和子域名【网站子域扫描工具01】 在Python中&#xff0c;有一些流行的漏洞扫描库可以对子域进行漏洞扫描和评估&#xff0c;比如Nmap、Sublist3r等。 1.端口扫描 以下是一个简单的示例代码&#xff0c;展示了如何使用Nmap进行基本的端口扫…

无法解析服务器的名称或地址/Wsl/0x80072eff/win10 WSL2问题解决Wsl 0x800701bc/Wsl:0x80041002

无法解析服务器的名称或地址 和 Wsl/0x80072eff 1.连VPN&#xff0c;推荐的VPN如下。(如一直显示无法连接&#xff0c;则推荐使用VPN) Anycast加速器 (any4ga.com) 优点&#xff1a;无限GB 缺点&#xff1a;较贵&#xff0c;通过银行卡充值9折后的价格是每月45元左右 …

DC-1靶机刷题记录

靶机下载地址&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1GX7qOamdNx01622EYUBSow?pwd9nyo 提取码&#xff1a;9nyo 参考答案&#xff1a; https://c3ting.com/archives/kai-qi-vulnhnbshua-tiDC-1.pdf【【基础向】超详解vulnhub靶场DC-1】 https://www.bilibi…

加解密算法整理(对称加密、非堆成加密、散列函数)

加解密算法是现代密码学核心技术&#xff0c;从设计理念和应用场景上可以分为两大基本类型&#xff0c;如下表所示。 算法类型特点优势缺陷代表算法对称加密加解密的密钥相同计算效率高&#xff0c;加密强度高需提前共享密钥&#xff0c;易泄露DES、3DES、AES、IDEA非对称加密…

Win10下在Qt项目中配置SQlite3环境

资源下载 官网资源&#xff1a;SQLite Download Page 1、sqlite.h sqlite-amalgamation-3450000.zip (2.60 MiB) 2、sqlite3.def&#xff0c;sqlite3.dll sqlite-dll-win-x64-3450000.zip (1.25 MiB) 3、 win10下安装sqlite3所需要文件 sqlite-tools-win-x64-3450000.zipht…

SpringCloud GateWay 在全局过滤器中注入OpenFeign网关后无法启动

目录 一、问题 二、原因 1、修改配置 2、添加Lazy注解在client上面 3、启动成功 一、问题 当在gateway的全局过滤器GlobalFilter中注入OpenFeign接口的时候会一直卡在路由中&#xff0c;但是不会进一步&#xff0c;导致启动未成功也未报错失败 2024-01-18 22:06:59.299 I…

EMQX安装和Java使用

一、EMQX介绍 EMQX是大规模分布式MQTT消息服务器&#xff0c;可以高效可靠连接海量物联网设备&#xff0c;实时处理分发消息与事件流数据&#xff0c;助力构建关键业务的物联网与云应用。EMQX 作为物联网应用开发和物联网平台搭建必须用到的基础设施软件&#xff0c;主要在边缘…

AI大模型学习笔记之二:什么是 AI 大模型的训练和推理?

在人工智能&#xff08;AI&#xff09;的领域中&#xff0c;我们经常听到训练&#xff08;Training) 和 推理&#xff08;Inference) 这两个词汇&#xff0c;它们是构建强大 AI 模型的关键步骤。我们通过类比人类的学习过程来理解这两个概念&#xff0c;可以更加自然而生动地理…

迅为RK3588开发板编译 Buildroot单独编译图形化界面(打包镜像)

上面 Kernel/U-Boot/Recovery/Rootfs 各个部分的编译后&#xff0c;将打包要用到的所有固件移动到 rockdev 目录下&#xff0c;然后打包为完整的 update.img 镜像。 首先在 linux 源码目录下输入以下命令进入编译的 UI 界面&#xff0c;进入之后如下所示&#xff1a; ./buil…

Flutter:跨平台移动应用开发的未来

Flutter&#xff1a;跨平台移动应用开发的未来 引言 Flutter的背景和概述 Flutter是由Google开发的一个开源UI工具包&#xff0c;用于构建漂亮、快速且高度可定制的移动应用程序。它于2017年首次发布&#xff0c;并迅速引起了开发者们的关注。Flutter采用了一种全新的方法来…

C++大学教程(第九版)5.19求Π的值

题目 代码 #include <bits/stdc.h> using namespace std;int main() {double pai 0;for (int count 1, i 1; count < 1000; i 2, count){int flag 1;if (count % 2 0){flag -1;}pai flag * (4.0 / (i * 1.0));cout << "当取前" << co…

C++ 之LeetCode刷题记录(十三)

&#x1f604;&#x1f60a;&#x1f606;&#x1f603;&#x1f604;&#x1f60a;&#x1f606;&#x1f603; 开始cpp刷题之旅。 依旧是追求耗时0s的一天。 70. 爬楼梯 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可…

监控系统——Zabbix

目录 Zabbix概述 Zabbix 监控原理 Zabbix 与 Prometheus的区别 Zabbix 6.0 新特性 Zabbix 6.0 功能组件 Zabbix Server 数据库 Web 界面 Zabbix Agent Zabbix Proxy Java Gateway Zabbix 6.0 部署 部署 zabbix 服务端 添加 zabbix 客户端主机 自定义监控内容…

#LLMOps##AIGC# Dify_构建本地知识库问答应用-生成Al应用的创新引擎 用于构建助手API和GPT的开源开发平台

github&#xff1a; https://github.com/langgenius/dify/blob/main/README_CN.md 介绍文档&#xff1a;https://docs.dify.ai/getting-started/readme Dify 介绍 Dify 笔记 Dify 是什么&#xff1f; 开源的大语言模型&#xff08;LLM&#xff09;应用开发平台融合了后端即服…

vue二次封装ant-design-vue中的Modal弹窗组件,实现拖拽,全屏两种功能,原有参数属性不变

在我们的项目的有的地方需要用弹框的拖拽&#xff0c;以及弹窗自定义全屏显示的需求&#xff0c;所以再次将二次合一&#xff0c;同时弹框里面内容自适应屏幕高度 在ant-design-vue中&#xff0c;已经实现了拖拽&#xff0c;全屏的功能&#xff0c;下面是ant官网的示例 自定义…

关键信息基础设施安全相关材料汇总

文章目录 前言一、法律(1)《中华人民共和国国家安全法》(2)《中华人民共和国网络安全法》(3) 《中华人民共和国密码法》(4)《中华人民共和国数据安全法》(5) 《中华人民共和国个人信息保护法》二、行政法规(6)《中华人民共和国保守国家秘密法实施条例》(7) 《关键信息基础设施安…

解决Python安装库时出现的Requirement already satisfied问题

uirement already satisfied的问题当我们用pip install 库名时&#xff0c;出现了下面 Requirement already satisfied WARNING: Ignoring invalid distribution -ip 的问题 对于这样的问题&#xff0c;解决办法就是在 pip install 后加 - -target你所要添加的库文件地址(注意…

python2实现数据库表定时全量同步sftp

python2实现数据库表定时全量同步sftp 需求 周边系统需要通过sftp接口&#xff0c;将本系统数据库的8张表吐给sftp&#xff0c;文件名为txt,提供的字段用#号分隔&#xff08;逗号存在分隔不开的情况&#xff09;&#xff0c;8张表采用全量每天同步。 环境 操作系统centos7.…