thymeleaf发送post请求的两种方式

简介: thymeleaf发送post请求的两种方式

这里简述下thymeleaf发送post请求的两种方式。

场景分析

我们知道,html不支持通过链接发送post请求,默认只能通过表单方式发送。不过这里我们使用的是thymeleaf,它还提供了一种通过方言(Dialect)、自定义tag处理器(AbstractProcessorDialect)的方式,我们可以通过就可以实现以链接的方式来发送post请求。下面我们来看具体实现:

我们先定义目标页面和对应的Controller:

post-request.html:


<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>

<meta charset="UTF-8">

<script th:src="@{/webjars/jquery/3.1.1/jquery.min.js}"></script>

<script th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>

<title>这是普通的post请求</title>

</head>

<body>

<div>

<p>Post请求响应:</p>

<p th:text="${response}"></p>

<p th:text="${post111}"></p>

</div>

<script>

</script>

</body>

</html>

post-request.html对应的Controller:


@Controller

public class WebController {

/**

* 普通的post请求

* 本地访问内容地址 :http://localhost:8201/request-methods

*

* @param map

* @return

*/

@PostMapping("/post-request")

public String postRequest(Model model, HashMap<String, Object> map, @RequestParam(value = "name", required = false, defaultValue = "猫了个咪") String name) {

model.addAttribute("response", name);

model.addAttribute("post111", "我是post方法");

return "post-request";

}

}

form表单方式发送post请求

在html中使用表单即可:


<div>

<h3>post请求</h3>

1、使用表单发送post请求:html不支持通过链接发送post请求,只能通过表单方式发送。

<form method="POST" th:action="@{/post-request}">

<input type="text" name="name" id="nameId"/>

<button type="submit" name="submit" value="value" class="button">发起普通post请求</button>

</form>

</div>

通过方言(Dialect)、自定义tag处理器(AbstractProcessorDialect)的方法来发送post请求

我们先自定义tag处理器:


public class LinkMethodAttrProcessor extends AbstractAttributeTagProcessor {

private static final String ATTR_NAME = "linkMethod";

private static final int PRECEDENCE = 10000;

public LinkMethodAttrProcessor(final String dialectPrefix) {

super(

TemplateMode.HTML, // This processor will apply only to HTML mode

dialectPrefix, // Prefix to be applied to name for matching

null, // No tag name: match any tag name

false, // No prefix to be applied to tag name

ATTR_NAME, // Name of the attribute that will be matched

true, // Apply dialect prefix to attribute name

PRECEDENCE, // Precedence (inside dialect's own precedence)

true); // Remove the matched attribute afterwards

}

@Override

protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag,

final AttributeName attributeName, final String attributeValue,

final IElementTagStructureHandler structureHandler) {

// get the method name (tag parameter)

final IEngineConfiguration configuration = context.getConfiguration();

final IStandardExpressionParser parser = StandardExpressions.getExpressionParser(configuration);

final IStandardExpression expression = parser.parseExpression(context, attributeValue);

final String method = (String) expression.execute(context);

// add custom javascript to change link method

final String link = tag.getAttribute("href").getValue();

final String action = "$('<form action=&quot;" + link + "" method="" + method + ""></form>').appendTo('body').submit(); return false;";

structureHandler.setAttribute("onclick", action);

structureHandler.setAttribute("href", "#");

}

}

然后将它添加进方言(Dialect):


public class CustomDialect extends AbstractProcessorDialect {

//定义方言名称

private static final String DIALECT_NAME = "Custom Dialect";

public CustomDialect() {

//设置自定义方言与"方言处理器"优先级相同

super(DIALECT_NAME, "custom", StandardDialect.PROCESSOR_PRECEDENCE);

}

@Override

public Set<IProcessor> getProcessors(String dialectPrefix) {

Set<IProcessor> processors = new HashSet<IProcessor>();

processors.add(new LinkMethodAttrProcessor(dialectPrefix));

processors.add(new StandardXmlNsTagProcessor(TemplateMode.HTML, dialectPrefix));

return processors;

}

}

接着在html标签中使用:


<div>

2、使用thymeleaf提供的方言,自定义标签处理来实现。

<p>

<!-- custom 跟 CustomDialect 中定义的 prefix 名称对应 -->

<a th:href="@{/post-request}" custom:linkMethod="post">发起普通post请求</a>

</p>

</div>

大功告成。

项目地址

tinytongtong / spring-thymeleaf

参考:

Can I make HTTP POST request from Thymeleaf table in Spring Boot application

springboot + Thymeleaf自定义标签

Creating our own Dialect

相关文章
Springboot之Thymeleaf 表单提交
Springboot之Thymeleaf 表单提交
969 0
|
XML 缓存 前端开发
Thymeleaf一篇就够了
Thymeleaf是Springboot官方支持的模板引擎,有着动静分离等独有特点,通过本文简单学习下吧!
65535 25
Thymeleaf一篇就够了
|
SQL 算法 开发者
Qoder Next:智能预测编码意图,精准化解开发难题
阿里巴巴旗下的智能编码平台 Qoder 正式发布其全新品牌 NEXT,并推出了基于自研 NEXT 模型的高阶智能补全能力 。 这一发布不仅仅是产品版本的更迭,它标志着 AI 辅助编程正式从“代码续写”阶段迈向“意图感知与自主代理”的 Agentic 编码新纪元 。 Qoder NEXT 通过结合抽象语法树(AST)精准模拟、ActionRL 强化学习算法以及海量真实编辑行为的学习,构建了一个能够主动感知代码库、理解开发者编辑历史的深度认知模型 。
1409 10
Qoder Next:智能预测编码意图,精准化解开发难题
|
安全 网络协议 Ubuntu
【常见开源库的二次开发】HTTP之libcurl库——libcurl使用(二)
【常见开源库的二次开发】HTTP之libcurl库——libcurl使用(二)
4658 3
|
JSON 前端开发 Java
List<String> 如何传参
List<String> 如何传参
2139 0
|
IDE Shell 开发工具
灵码使用体验
上周使用了通义灵码三天,分享一下体验。相较于Trae、VSCode和CodeBuddy,灵码存在一些不足:响应速度较慢,生成代码效率低;汉化不够完善,菜单仍为英文;纠错能力弱,无法有效提示代码问题;Shell集成效果差,终端命令错误处理不佳;MCP工具集成不如Trae便捷。不过,灵码也有亮点:支持超长上下文输入,有助于精确开发;Qwen3推理能力强,能较好理解用户意图并编辑代码。希望后续更新能优化这些问题,提升用户体验。
3888 0
|
算法 vr&ar Python
一文速学-时间序列分析算法之移动平均模型(MA)详解+Python实例代码
一文速学-时间序列分析算法之移动平均模型(MA)详解+Python实例代码
4320 0
一文速学-时间序列分析算法之移动平均模型(MA)详解+Python实例代码
|
存储 搜索推荐 算法
【数据结构】树型结构详解 + 堆的实现(c语言)(附源码)
本文介绍了树和二叉树的基本概念及结构,重点讲解了堆这一重要的数据结构。堆是一种特殊的完全二叉树,常用于实现优先队列和高效的排序算法(如堆排序)。文章详细描述了堆的性质、存储方式及其实现方法,包括插入、删除和取堆顶数据等操作的具体实现。通过这些内容,读者可以全面了解堆的原理和应用。
1082 16
|
人工智能 开发者 Python
python读取word文档 | AI应用开发
在RAG系统中,构建知识库时需读取多种外部文档,其中Word文档较为常见。本文介绍如何使用`python-docx`库读取Word文档(.docx格式)中的标题、段落、表格和图片等内容。首先通过`pip install python-docx`安装库,然后利用提供的接口提取所需信息。尽管该库功能强大,但在识别标题样式时需自定义逻辑,并且仅提供图片的URI而非直接加载。示例代码展示了读取文本、识别标题、读取表格及获取图片URI的方法。【10月更文挑战第2天】
1891 2
|
自然语言处理 IDE 测试技术
阿里云 的 通义灵码 在 IDEA 使用
【7月更文挑战第1天】在IntelliJ IDEA中启用阿里云的通义灵码插件,需安装插件,搜索“tongyi”安装并登录阿里云账号。插件提供行级/函数级代码建议、自然语言生成代码、单元测试生成、代码注释和解释等功能。支持多种编程语言,但需联网使用并依赖阿里云账号。快捷键在macOS和Windows上有差异,如macOS的“Tab”键接受代码建议,“Esc”废弃。
6851 0