Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Thymeleaf将参数从html发送到控制器

Thymeleaf将参数从html发送到控制器
EN

Stack Overflow用户
提问于 2014-04-01 18:34:02
回答 3查看 53.1K关注 0票数 8

我是百里香的新手。我正在尝试创建简单的crud应用程序。我正在尝试删除删除按钮上的Customer类的对象。如何将参数(例如- id)设置为使用Thymeleaf调用deleteUser的方法。这是我的控制器。

代码语言:javascript
运行
AI代码解释
复制
package controllers;

//imports


@Controller
public class WebController extends WebMvcConfigurerAdapter {

    @Autowired
    private CustomerDAO customerDAO;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/results").setViewName("results");
    }

    //show all users
    @RequestMapping(value="/users", method=RequestMethod.GET)
    public String contacts(Model model) {
        model.addAttribute("users",customerDAO.findAll());
        return "list";
    }

    //show form
    @RequestMapping(value="/users/add", method=RequestMethod.GET)
    public String showForm(Customer customer) {
        return "form";
    }

    //add user
    @RequestMapping(value="/users/doAdd", method=RequestMethod.POST)
    public String addUser(@RequestParam("firstName") String firstName,
                           @RequestParam("lastName") String lastName,
                           @RequestParam("lastName") String email) {
        customerDAO.save(new Customer(firstName, lastName, email));
        return "redirect:/users";
    }

    //delete user
    @RequestMapping(value="users/doDelete/{id}", method = RequestMethod.POST)
    public String deleteUser (@PathVariable Long id) {
        customerDAO.delete(id);
        return "redirect:/users";
    }
}

这是我的观点。

代码语言:javascript
运行
AI代码解释
复制
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
List of users
<a href="users/add">Add new user</a>
<table>
    <tr>
        <th>Id</th>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email</th>
        <th>Action</th>
    </tr>
    <tr th:each="user : ${users}">
        <td th:text="${user.id}">Id</td>
        <td th:text="${user.firstName}">First name</td>
        <td th:text="${user.lastName}">Last Name</td>
        <td th:text="${user.email}">Email</td>
        <td>
            <form th:action="@{/users/doDelete/}" th:object="${customer}" method="post">
                <button type="submit">Delete</button>
            </form>
        </td>
    </tr>
</table>
</body>
</html>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-04-02 15:05:43

执行此操作不需要表单:

代码语言:javascript
运行
AI代码解释
复制
<td>
    <a th:href="@{'/users/doDelete/' + ${user.id}}">
        <span>Delete</span>
    </a>
</td>

票数 14
EN

Stack Overflow用户

发布于 2014-04-03 20:55:34

Blejzer answer是一个简单而直接的解决方案,除非你还在使用spring安全(总是推荐),在这种情况下,你应该更喜欢POST而不是GET来进行所有的修改操作,比如delete,以防止CSRF攻击。这就是为什么spring recommends logout只能这样做的原因。为了适应POST,将您的控制器更改为从请求参数而不是路径变量读取此参数

代码语言:javascript
运行
AI代码解释
复制
//delete user
@RequestMapping(value="users/doDelete", method = RequestMethod.POST)
public String deleteUser (@RequestParam Long id) {
    customerDAO.delete(id);
    return "redirect:/users";
}

在表单中添加一个隐藏字段,该字段以id作为名称并在隐藏参数中显示它的值

代码语言:javascript
运行
AI代码解释
复制
<form th:action="@{/users/doDelete}" th:object="${customer}" method="post">
    <input type="hidden" th:field="${id}" />
    <button type="submit">Delete</button>
</form>

顺便说一句,即使你没有使用spring security,也总是推荐使用post来进行任何实体修改操作(比如删除或更新)。从长远来看,让你在网络上省去了很多麻烦。有关详细信息,请查看Quick Checklist for Choosing HTTP GET or POST

票数 14
EN

Stack Overflow用户

发布于 2016-10-13 11:25:23

path变量可以设置为:

代码语言:javascript
运行
AI代码解释
复制
<a th:href="@{/users/doDelete/__${user.id}__}"><span>Delete</span></a>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22794057

复制
相关文章
thymeleaf动态渲染html
我们可以使用thymeleaf进行动态渲染html内容 假设我的整个页面都是字符串,例如如下格式: "<!DOCTYPE html>\n" + "<html>\n" + " <head>\n" + " <meta charset=\"utf-8\">\n" + " <title></title>\n" + " </head>\n" + " <body>\n" + " </body>\n" + "</html>
阿超
2022/08/21
2.6K0
thymeleaf动态渲染html
从ajax获取参数到控制器Laravel
route Route::get('search', 'ShowstaffController@search'); 接收变量
双面人
2020/02/17
1.3K0
Thymeleaf HTML5模板引擎
Thymeleaf的模板还可以用作工作原型,Thymeleaf会在运行期替换掉静态值。例如下面的html文件,当作为静态文件时,product name显示为Red Chair,当运行在容器中并提供product这个对象时,product name的值会自动替换为product.description对应的值。
王念博客
2019/07/25
2.4K0
Jenkins添加html报告并发送到邮箱
1、插件安装 1、Manage Jenkins ---> Manage Plugins ---> Available 搜索框输入下面几款插件 2、插件安装以及插件的说明: 1) HTML Publisher plugin # 推送生成的html报告到jenkins 2) Email Extension Plugin # jenkins配置email的插件 3) Groovy # 让jenkins可以识别css样式 PS:无法安装插件请点
cywhat
2022/12/08
1.2K0
Jenkins添加html报告并发送到邮箱
SpringBoot当中如何整合动态html模板:Thymeleaf
4.整合动态html模板:Thymeleaf: 光是静态html还不足够,必须html还能显示动态成分,这时我们考虑使用thymeleaf,就能完全达到springmvc的水平了,官方推荐thymeleaf。继续在上一部分的项目中,在src/main目录下,添加resources/templates/result.html:(参考目录下:bootThymeleaf) 例4.1: 1)首先在pom.xml中添加: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 注意:即使导了上面的包,也没有办法访问到resources根目录下的html。至于templates目录下的html,直接或sendRedirect都不能访问。唯有用下面的方法访问。 package com.SpringbootMaven; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @SpringBootApplication public class App { @RequestMapping("/hello") public String myhome(HttpServletResponse res,HttpSession session) throws IOException { System.out.println("spring boot springMvc 马克-to-win!"); session.setAttribute("user","马克-to-win 马克java社区创始人"); return "result"; /*下列不能再用了,在Thymeleaf框架中,sendRedirect不能跳到templates目录里的html*/ // res.sendRedirect("result.html"); } public static void main(String[] args) throws Exception { SpringApplication.run(App.class, args); } } index.html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> index1 <a href="/hello.do">test SpringMvc</a> </body> </html>
马克java社区
2019/07/27
1.8K0
SpringBoot当中如何整合动态html模板:Thymeleaf
Thymeleaf从入门到吃灰
然后编写一个springBoot的启动类,这里不多说了,直接启动项目访问测试即可:
良月柒
2020/06/30
2.7K0
Thymeleaf从入门到吃灰
Thymeleaf从入门到清晰使用
但是:springboot这个项目首先是以jar的方式,不是war,第二,我们用的还是嵌入式的Tomcat,所以他默认是不支持jsp,对此springboot推荐我们使用Thymeleaf模板引擎
秋名山码神
2023/01/13
9910
Thymeleaf从入门到清晰使用
将CSV的数据发送到kafka(java版)
前面的图可以看出,读取CSV再发送消息到kafka的操作是Java应用所为,因此今天的主要工作就是开发这个Java应用,并验证;
程序员欣宸
2021/04/16
3.6K0
将CSV的数据发送到kafka(java版)
如何将Flink应用的日志发送到kafka
产线环境上的Flink应用是长时运行的应用,日志量较大,通过flink web页面查看任务日志会导致浏览器卡死,通过日志排查问题非常不便。因此,需要将flink应用的日志发送到外部系统,方便进行日志检索
Eights
2020/09/10
2.4K0
如何将Flink应用的日志发送到kafka
使用 iptables 将 Kubernetes Service 流量随机发送到 Pod
本文将带大家了解 Kubernetes 的 kube-proxy 组件如何使用 iptables 将 service 流量随机发送到 Pod,目的是实现 service 所需的 iptables 规则。
CNCF
2021/04/21
1.7K0
spring boot用ModelAndView向Thymeleaf模板传参数
最近在调试一个Spring Boot向Thymeleaf模板传参数的例子,但踩了很多坑,这里就把详细过程记录下来,以供大家参考。
用户1153489
2021/03/02
3K1
ASP.NET MVC 5 - 将数据从控制器传递给视图
在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图。控制器类将响应请求来的URL。控制器类是给您写代码来处理传入请求的地方,并从数据库中检索数据,并最终决定什么类型的返回结果会发送回浏览器。视图模板可以被控制器用来产生格式化过的HTML从而返回给浏览器。 控制器负责给任何数据或者对象提供一个必需的视图模板,用这个视图模板来Render返回给浏览器的HTML。最佳做法是:一个视图模板应该永远不会执行业务逻辑或者直接和数据库进行交互。相应的,一个视图模板应该只和控制器所提供的数据进行交
葡萄城控件
2018/01/10
5.3K0
ASP.NET MVC 5 - 将数据从控制器传递给视图
了解 Spring MVC 架构、Dispatcher Servlet 和 JSP 文件的关键作用
在 Spring MVC 中,应用程序被分为三个主要组件:Model、View 和 Controller。Model 表示应用程序的数据和业务逻辑,View 负责生成发送到客户端 Web 浏览器的 HTML,而 Controller 充当 Model 和 View 之间的中介,处理传入的 HTTP 请求并生成适当的响应。
小万哥
2023/08/03
2060
了解 Spring MVC 架构、Dispatcher Servlet 和 JSP 文件的关键作用
构建fluentd镜像将收集的日志发送到elasticsearch
构建脚本: version: "3" services: fluentd: build: ./fluentd volumes: - ./fluentd/conf:/fluentd/etc privileged: true ports: - "24224:24224" - "24224:24224/udp" environment: - TZ=Asia/Shanghai restart: always
johnhuster的分享
2022/03/28
5610
ActFramework对模板引擎的支持与应用
ActFramework支持的模板引擎 目前ActFramework支持的模板引擎: Rythm (框架内置) - 引擎ID: rythm Beetl - 引擎ID: beetl FreeMarker - 引擎ID: freemarker Mustache - 引擎ID: mustache Thymeleaf - 引擎ID: thymeleaf Velocity - 引擎ID: velocity 注意 如果要使用Rythm以外的其他模板引擎需要在pom.xml文件中添加依赖: Beelt依赖: <depen
老码农
2018/06/27
8360
Html获取Url参数
做web前端的开发很多的时候都会遇到的一个很简答的问题,就是两个页面之间的跳转,一般来说是:
何处锦绣不灰堆
2020/05/29
10.1K0
Html获取Url参数
html传递中文参数
场景: 使用window.location.href="../../../cleangame/index.html?playgame=" + $('#email').val();跳转到项目内一个页面,
在下是首席架构师
2022/08/18
1.1K0
【SpringMVC】 一文掌握 》》》 @RequestMapping注解
由于前端控制器对浏览器发送的请求进行了统一的处理,但是具体的请求有不同的处理过程,因此需要创建处理具体请求的类,即请求控制器
.29.
2023/10/17
3590
【SpringMVC】 一文掌握 》》》 @RequestMapping注解
【springmvc thymeleaf】springmvc整合thymeleaf
Thymeleaf提供了一组Spring集成,使您可以将其用作Spring MVC应用程序中JSP的全功能替代品。
lomtom
2021/10/27
2.7K0
【springmvc thymeleaf】springmvc整合thymeleaf
Spring认证指南:了解如何使用 Spring Security 保护您的 Web 应用程序
本指南将引导您完成使用受 Spring Security 保护的资源创建简单 Web 应用程序的过程。
IT胶囊
2022/03/07
1.2K0
Spring认证指南:了解如何使用 Spring Security 保护您的 Web 应用程序

相似问题

将数据从html发送到Thymeleaf中的控制器?

10

无法将参数从html发送到控制器

23

Thymeleaf:从控制器到html

10

在单击按钮时,将参数从html/thymeleaf传递到弹簧控制器

13

将参数从html发送到控制器中的方法

24
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档