惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
有赞技术团队
有赞技术团队
博客园_首页
H
Help Net Security
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
The Cloudflare Blog
腾讯CDC
Jina AI
Jina AI
Last Week in AI
Last Week in AI
月光博客
月光博客
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
爱范儿
爱范儿
N
Netflix TechBlog - Medium
F
Fortinet All Blogs

回忆中的明天

自己写一个Web 端的MiMo TTS Chat,方便实现文本转语音,API限免中 · 回忆中的明天 NVIDIA NIM 开发平台,提供超多免费大模型 · 回忆中的明天 iTranslation 简单快捷的翻译软件,支持数十种语言互译 · 回忆中的明天 国内外免费大模型平台,支持 API 调用的超多免费大模型 · 回忆中的明天 SiliconFlow 硅基流动一站式大模型云服务平台,提供超多免费大模型 · 回忆中的明天 Xiaomi MiMo 小米大模型团队开发的大语言模型,开源限免中…… · 回忆中的明天 BigModel 智谱大模型开放平台,提供超多自研免费大模型 · 回忆中的明天 OpenRouter 模型聚合平台,提供超多免费模型使用 · 回忆中的明天 iReader 英语点读学习系统,译林小学英语在线点读 · 回忆中的明天 ZenMux 企业级大模型聚合平台,提供免费试用模型 Gemini 3 Pro · 回忆中的明天 英语学习,新概念英语在线点读、全文朗读学习系统 · 回忆中的明天 iGSTT(Gemini STT) 开源免费的语音转文本(STT)的命令行工具 · 回忆中的明天 Python 项目打包,并上传到 PyPI,分享项目 · 回忆中的明天 iGTTS(Gemini TTS) 开源免费的文本转语音(TTS)的命令行工具 · 回忆中的明天 iChat(AI Chat) 智能聊天工具,支持 MiMo、DeepSeek、Gemini、Grok、OpenAI和自定义AI · 回忆中的明天 NanoPi R2S 安装 Debian 固件系统,旁路由网络代理内网转发,决解直连网络卡顿 nftables · 回忆中的明天 NanoPi R2S Armbian Linux 旁路由网络代理内网转发,决解直连网络卡顿 iptables · 回忆中的明天 sing-box rule-set · 回忆中的明天 Xcode 最全最实用的快捷键列表 · 回忆中的明天 SwiftUI 中的@State、@Bindable和@Binding · 回忆中的明天 免费的图床服务器-GitHub Pages · 回忆中的明天 A Free Native Image Uploading Tool for macOS · 回忆中的明天 免费的图床服务器-Cloudflare-R2 · 回忆中的明天 图传 (iUploader) - macOS 免费原生图床上传利器 · 回忆中的明天 本地无法加载托管到Cloudflare中的图片等资源,权限错误403 · 回忆中的明天 国内、外免费公共的DNS,支持的DoH,防止污染、篡改的问题 · 回忆中的明天 macOS 系统下载和安装 · 回忆中的明天 使用 CURL 命令调试和诊断网络问题,网站请求测速 · 回忆中的明天 WARP Beta for macOS 支持新协议 MASQUE · 回忆中的明天 2024年自用国外靠谱的 VPS 服务器推荐 · 回忆中的明天
Spring 表单提交,Form Submission · 回忆中的明天
2021-05-30 · via 回忆中的明天

原文:https://ichochy.com/posts/spring/20210530.html


表单提交完成数据的交互,实现数据的传递并进行系统处理或反馈,完成用户的指令。

开发工具

  • IDEA: 2021.1.2
  • Java: 1.8
  • Spring Boot: 2.5

创建项目

New Project

打开 IDEA 创建新项目 New Project,使用 start.spring.io 快速构建 16226365527086133

添加依赖

添加 webthymeleaf 依赖,finish 创建项目 16231453775779659

pom.xml 中手动管理依赖模块

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>

编写项目

表单信息对象

创建 Form 对象,用来接收、传递表单数据

/*
 * Copyright (c) 2021 iChochy
 * URL:https://ichochy.com
 * Date:2021/06/10 19:36:10
 */

package com.ichochy.example.form;

/**
 * 表单信息
 */
public class Form {

    private long id;
    private String title;
    private String url;
    private String content;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

创建控制器

创建控制器 FormController,接收表单数据

/*
 * Copyright (c) 2021 iChochy
 * URL:https://ichochy.com
 * Date:2021/06/09 22:07:09
 */

package com.ichochy.example.form;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class FormController {

    /**
     * 打开表单
     * @param model
     * @return
     */
    @GetMapping(value = "/form")
    public String openForm(Model model) {
        model.addAttribute("form", new Form());
        return "form";
    }

    /**
     * 提交表单
     * @param form
     * @param model
     * @return
     */
    @PostMapping(value = "/form")
    public String submitForm(@ModelAttribute Form form, Model model) {
        model.addAttribute("form", form);
        return "result";
    }
}
  • Model 为前端视图模型,添加视图数据,供视图展示
  • @Controller 注解,告诉 Spring 该类(FormController)为请求控制器
  • @ModelAttribute 注解,告诉 Spring 该对象为视图请求参数对象
  • @GetMapping(“/form) 注解,告诉 Spring 该方法(openForm)来响应 http://localhost:8080/formget 请求。
  • @PostMapping(“/form) 注解,告诉 Spring 该方法(submitForm)来响应 http://localhost:8080/formget 请求。
  • 方法返回 String 为展示对应的视图名称,如:return "form"展示 form.html 视图

创建表单页面

新建 form.html,接交表单数据

<!--
  ~ Copyright (c) 2021 iChochy
  ~ URL:https://ichochy.com
  ~ Date:2021/06/10 19:44:10
  -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form</title>
</head>
<body>
<h1>Form</h1>
<form th:action="@{/form}" th:object="${form}" method="post">
    <p>Id: <br><input type="text" th:field="*{id}"/></p>
    <p>Title:<br> <input type="text" th:field="*{title}"/></p>
    <p>URL: <br><input type="text" th:field="*{url}"/></p>
    <p>Message: <br><textarea th:field="*{content}"></textarea></p>
    <p><input type="submit" value="Submit"/> <input type="reset" value="Reset"/></p>
</form>
</body>
</html>
  • th:action="@{/form}" 设置表单的请求地址
  • method=“post” 设置表单的请求方式为 post
  • th:object="${form}" 设置表单的请求数据对象
  • th:field="*{id}" 设置表单的请求参数属性

创建结果页面

新建 result.html,展示表单数据

<!--
  ~ Copyright (c) 2021 iChochy
  ~ URL:https://ichochy.com
  ~ Date:2021/06/10 19:44:10
  -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Result</title>
</head>
<body>
<h1>Form</h1>
<h1>Result</h1>
<p th:text="'Id: ' + ${form.id}"/>
<p th:text="'Title: ' + ${form.title}"/>
<p th:text="'URL: ' + ${form.url}"/>
<p th:text="'Message: ' + ${form.content}"/>
<a href="/form">Submit another message</a>
</body>
</html>
  • ${form.id} 获取数据对象属性值展示

项目目录

├── pom.xml
└──src
    └── main
        ├── java
        │   └── com
        │       └── ichochy
        │           └── example
        │               ├── ExampleApplication.java
        │               └── form
        │                   ├── Form.java
        │                   └── FormController.java
        └── resources
            ├── application.properties
            ├── static
            └── templates
                ├── form.html
                └── result.html

运行项目

Dubug 运行项目

启动成功后可以看到默认端口号为8080 162314892348552510

浏览器访问

直接访问:http://localhost:8080/form 162333229951452314

查看表单信息

Submit 提交表单后可以查看到表单提交的信息 162333234351368115

GitHub

https://github.com/iChochy/Example

引用