跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书AI学习GitHub 精选镜像AI工具UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
Java大前端java

Spring Boot Web 三大核心交互实战:表单、AJAX 与 JSON

Spring Boot Web 开发涉及三种主流交互模式。本教程通过求和计算器演示表单参数绑定,利用 Session 实现无刷新登录状态管理,并通过 RESTful 接口完成 JSON 数据交互。重点解析 @RequestBody 注解使用、前后端字段匹配及 AJAX 异步请求配置,帮助开发者掌握从传统页面跳转至现代前后端分离的核心联调技巧。

星落发布于 2026/3/27更新于 2026/10/2184 浏览
Spring Boot Web 三大核心交互实战:表单、AJAX 与 JSON

Spring Boot Web 三大核心交互实战

作为后端开发,理解前后端如何'对话'是基本功。我们通常通过三种典型场景来掌握这些交互:表单提交、AJAX 异步登录以及JSON 数据交互。下面直接看代码和关键点。

1. 基础交互:表单提交与参数绑定

这是最传统的模式,适合简单的页面跳转场景。我们以一个求和计算器为例。

后端实现

使用 @RestController 简化接口,Spring MVC 会自动将请求参数映射到方法参数上。

package cn.overthinker.springboot;

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

@RequestMapping("/calc")
@RestController
public class CalcController {
    /**
     * 求和接口:通过方法参数名自动接收前端表单提交的 num1 和 num2
     */
    @RequestMapping("/sum")
    public String sum(Integer num1, Integer num2) {
        // 使用 Integer 包装类进行非空判断,避免空指针异常
        if (num1 == null || num2 == null) {
            return "请求非法:请输入两个数字!";
        }
        // 计算并返回结果
        return "计算结果为:" + (num1 + num2);
    }
}

前端页面

HTML 表单的 name 属性必须与后端参数名完全一致。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    < = =>
    简单求和计算器
    


    
        简单求和计算器
        
            数字 1:
            数字 2:
            
        
    


目录

  1. Spring Boot Web 三大核心交互实战
  2. 1. 基础交互:表单提交与参数绑定
  3. 后端实现
  4. 前端页面
  5. 2. 进阶交互:AJAX 异步登录与 Session 管理
  6. 后端逻辑
  7. 前端实现
  8. 3. 现代交互:JSON 数据传输与 RESTful 接口
  9. 后端 DTO 与 Controller
  10. 前端发送 JSON
  11. 总结:三种模式对比
meta
name
"viewport"
content
"width=device-width, initial-scale=1.0"
<title>
</title>
<style>
body { font-family: sans-serif; background-color: #f4f7f6; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; } .calculator-container { background-color: #ffffff; padding: 40px; border-radius: 12px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.1); width: 300px; text-align: center; } h1 { color: #333; margin-bottom: 30px; font-size: 24px; border-bottom: 2px solid #5cb85c; display: inline-block; padding-bottom: 5px; } input[type="text"] { width: 100%; padding: 10px; margin-bottom: 10px; border: 1px solid #ccc; border-radius: 6px; box-sizing: border-box; } input[type="submit"] { background-color: #5cb85c; color: white; padding: 12px 20px; border: none; border-radius: 6px; cursor: pointer; font-size: 16px; margin-top: 20px; width: 100%; transition: background-color 0.3s ease; } input[type="submit"]:hover { background-color: #4cae4c; }
</style>
</head>
<body>
<div class="calculator-container">
<h1>
</h1>
<form action="/calc/sum" method="post">
<input name="num1" type="text" placeholder="请输入数字 1">
<br>
<input name="num2" type="text" placeholder="请输入数字 2">
<br>
<input type="submit" value="点击相加">
</form>
</div>
</body>
</html>

求和计算器界面

联调要点:

  • 前端 <input name="num1"> 的 name 必须与后端 Integer num1 完全一致。
  • Spring Boot 会自动处理字符串到 Integer 的类型转换。

2. 进阶交互:AJAX 异步登录与 Session 管理

现代应用很少整页刷新,我们常用 AJAX 配合 Session 保持用户状态。

后端逻辑

登录成功后,将用户名存入 HttpSession。后续请求携带 Session ID 即可识别身份。

package cn.overthinker.springboot;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    /**
     * 登录接口:使用 HttpSession 存储用户信息
     */
    @PostMapping("/login")
    public boolean login(String userName, String password, HttpSession session) {
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        // 硬编码校验(实际项目应查询数据库)
        if ("admin".equals(userName) && "123456".equals(password)) {
            // 核心知识点:登录成功后,将用户名存入 Session
            session.setAttribute("loginUser", userName);
            return true;
        }
        return false;
    }

    /**
     * 获取当前登录用户接口:从 Session 中读取用户信息
     */
    @GetMapping("/getLoginUser")
    public String getLoginUser(HttpServletRequest request) {
        // request.getSession(false):如果 Session 不存在,则不创建
        HttpSession session = request.getSession(false);
        if (session != null) {
            String loginUser = (String) session.getAttribute("loginUser");
            return loginUser;
        }
        return "";
    }
}

前端实现

使用 jQuery 发起 AJAX 请求,无需刷新页面即可跳转或提示。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
    <style>
        body { font-family: sans-serif; background-color: #e8eff1; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; }
        .login-box { background-color: #fff; padding: 40px; border-radius: 8px; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); width: 280px; text-align: center; }
        h1 { color: #3c8dbc; margin-bottom: 25px; }
        input[type="text"], input[type="password"] { width: 100%; padding: 10px; margin-bottom: 15px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
        input[type="button"] { background-color: #3c8dbc; color: white; padding: 10px 15px; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; width: 100%; transition: background-color 0.3s; }
        input[type="button"]:hover { background-color: #367fa9; }
    </style>
</head>
<body>
    <div class="login-box">
        <h1>用户登录</h1>
        用户名:<input name="userName" type="text" id="userName" placeholder="请输入用户名"><br>
        密码:<input name="password" type="password" id="password" placeholder="请输入密码"><br>
        <input type="button" value="登录" onclick="login()">
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script>
        function login() {
            $.ajax({
                url: "/user/login",
                type: "post",
                data: {
                    userName: $("#userName").val(),
                    password: $("#password").val()
                },
                success: function(result) {
                    if (result) {
                        location.href = "/index.html";
                    } else {
                        alert("用户名或密码错误");
                    }
                }
            });
        }
    </script>
</body>
</html>

登录界面

原理说明: 浏览器通过 Cookie 自动携带 Session ID。服务器根据 ID 找到对应的 Session 对象,从而取出之前存储的 loginUser 信息。


3. 现代交互:JSON 数据传输与 RESTful 接口

前后端分离架构下,JSON 是最通用的数据格式。这里演示一个简单的留言板功能。

后端 DTO 与 Controller

使用 @RequestBody 接收 JSON 字符串并自动反序列化为 Java 对象。

package cn.overthinker.springboot;

import lombok.Data;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

@Data
public class MesseageInfo {
    private String from;
    private String to;
    private String message;
}

@RequestMapping("/Message")
@RestController
public class MessageController {
    private List<MesseageInfo> messeageInfoList = new ArrayList<>();

    /**
     * 发布留言接口:使用 @RequestBody 接收 JSON 数据
     */
    @PostMapping("/publish")
    public Boolean publish(@RequestBody MesseageInfo messeageInfo) {
        if (!StringUtils.hasLength(messeageInfo.getFrom()) || !StringUtils.hasLength(messeageInfo.getTo()) || !StringUtils.hasLength(messeageInfo.getMessage())) {
            return false;
        }
        messeageInfoList.add(messeageInfo);
        return true;
    }

    /**
     * 获取留言列表接口:返回 JSON 数组
     */
    @GetMapping("/getList")
    public List<MesseageInfo> getList() {
        return messeageInfoList;
    }
}

前端发送 JSON

注意 contentType 设置和 JSON.stringify 的使用。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>留言板</title>
    <style>
        body { font-family: sans-serif; background-color: #f0f7f4; padding: 20px; }
        .container { width: 400px; margin: 20px auto; background-color: #fff; padding: 25px; border-radius: 10px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08); text-align: center; }
        h1 { color: #387063; margin-bottom: 5px; }
        .grey { color: #888; margin-bottom: 20px; }
        .row { display: flex; justify-content: space-between; align-items: center; height: 40px; margin-bottom: 10px; }
        .row span { width: 70px; text-align: left; color: #555; font-weight: bold; }
        .row input { flex-grow: 1; height: 35px; padding: 5px 10px; border: 1px solid #ddd; border-radius: 4px; }
        #submit { width: 100%; height: 45px; background-color: #387063; color: white; border: none; border-radius: 5px; margin-top: 20px; font-size: 18px; cursor: pointer; transition: background-color 0.3s; }
        #submit:hover { background-color: #2b574d; }
        .message-list div { text-align: left; padding: 8px 0; border-bottom: 1px dashed #eee; color: #333; }
    </style>
</head>
<body>
    <div class="container">
        <h1>留言板</h1>
        <p class="grey">输入后点击提交,信息将显示在下方</p>
        <div class="row"><span>谁:</span><input type="text" id="from" placeholder="你的名字"></div>
        <div class="row"><span>对谁:</span><input type="text" id="to" placeholder="你想对谁说"></div>
        <div class="row"><span>说什么:</span><input type="text" id="say" placeholder="你的留言内容"></div>
        <input type="button" value="提交留言" id="submit" onclick="submit()">
        <div class="message-list"></div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script>
        function loadMessages() {
            $.ajax({
                type: "get",
                url: "/Message/getList",
                success: function(messages) {
                    $(".message-list").empty();
                    for (let msg of messages) {
                        let divE = "<div>" + msg.from + " 对 " + msg.to + " 说:" + msg.message + "</div>";
                        $(".message-list").append(divE);
                    }
                }
            });
        }
        loadMessages();
        function submit() {
            var from = $('#from').val();
            var to = $('#to').val();
            var say = $('#say').val();
            if (from == '' || to == '' || say == '') return;
            $.ajax({
                type: "post",
                url: "/Message/publish",
                : ,
                : .({
                    : ,
                    : to,
                    : say
                }),
                : () {
                     (result) {
                        ();
                        $().();
                        $().();
                        $().();
                    }  {
                        ();
                    }
                }
            });
        }
    </script>
</body>
</html>

留言板界面

关键细节:

  1. @RequestBody:告诉 Spring 把 Request Body 里的 JSON 解析成对象。
  2. Content-Type:前端必须设为 application/json。
  3. 字段匹配:JSON 的 Key 必须与 DTO 的属性名一致(如 message)。

总结:三种模式对比

联调模式适用场景核心机制优点缺点
Form 表单提交简单工具、旧式系统浏览器跳转/刷新简单、无需 JS体验差、无法精细控制
AJAX (Query)登录、搜索异步通信无刷新、体验好仅适合少量数据
AJAX (JSON)复杂业务、前后端分离@RequestBody 接收 DTO传输结构数据、灵活需配置 Content-Type

掌握这三种模式,基本能覆盖 90% 的 Web 交互需求。

更多推荐文章

查看全部
  • Uncaught TypeError: Cannot read properties of undefined 报错根源与根治方案
  • 张一鸣:招聘人才需具备的三个核心标准
  • Kotlin 注解详解:声明、应用与元注解
  • 从编译器优化视角看C++ explicit关键字的深层影响
  • 全自动 AI 视频创作与发布工具:LuoGen-agent
contentType
"application/json"
data
JSON
stringify
from
from
to
message
success
function
result
if
loadMessages
'#from'
val
""
'#to'
val
""
'#say'
val
""
else
alert
"添加留言失败,请检查输入"
  • 网络安全行业前景分析与 Java 开发对比及入门指南
  • Python爬虫入门:阶段学习路线与技术要点
  • 数据结构:查找算法与二叉排序树详解
  • LeetCode 最长公共前缀:三种解法详解
  • 并查集数据结构详解与实战应用
  • 自然语言处理在教育领域的应用与实战
  • 深度学习模型优化策略与实战调参
  • 实战演练:基于 Next.js 和 Wagmi 构建支持 TokenP 钱包登录的 DApp 前端
  • OpenClaw 多平台卸载指南:Windows、macOS、Linux 及包管理器
  • AI 辅助前端开发:掌握三大设计技能独立完成产品全流程
  • C++ 如何计算类的大小
  • Python 爬虫逆向兼职实战指南
  • 基于 rocketmq-client-python 构建高效 RocketMQ 消息系统
  • Flutter 与 React Native 跨平台移动应用开发技术对比指南
  • Browser-Use + LightRAG Agent:利用 LLM 实现高效网页数据抓取
  • 相关免费在线工具

    • Keycode 信息

      查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online

    • Escape 与 Native 编解码

      JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online

    • JavaScript / HTML 格式化

      使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online

    • JavaScript 压缩与混淆

      Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online

    • Base64 字符串编码/解码

      将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

    • Base64 文件转换器

      将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online