学习Thymeleaf风格的HTML表单进行数据绑定

前端代码

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/login}" th:object="${loginBean}" method="post">
    <!--th:field="*{uname}"的uname与实体类的属性相同  -->
    <p>Uname: <input type="text" th:field="*{uname}" th:placeholder="请输入用户名"/></p>
    <p>Urole: <input type="text" th:field="*{urole}" th:placeholder="请输入角色"/></p>
    <p><input type="submit" value="Submit"/> <input type="reset" value="Reset"/></p>
</form>
</body>
</html>

后端请求和处理方法

package com.ch.ch5_1.controller;

import com.ch.ch5_1.model.LoginBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {
    @RequestMapping("/toLogin")
    public String toLogin(Model model) {
        model.addAttribute("loginBean", new LoginBean());
        return "login";
    }

    @RequestMapping("/login")
    public String greetingSubmit(@ModelAttribute LoginBean loginBean) {
        System.out.println("测试提交的数据:" + loginBean.getUname());
        return "result";
    }
}

代码学习解释

这段HTML代码使用了Thymeleaf模板引擎的语法,用于创建一个简单的表单页面。Thymeleaf是一个流行的模板引擎,用于在Java应用程序中处理模板渲染。

让我们逐个解释HTML代码中的关键部分:

  1. <!DOCTYPE html>: 声明文档类型为HTML5。

  2. xmlns:th="http://www.thymeleaf.org": 这是一个XML命名空间声明,用于指定Thymeleaf模板引擎的命名空间。它允许在HTML中使用Thymeleaf的特殊语法。

  3. <head>: 页面头部部分。

  4. <meta charset="UTF-8">: 指定页面的字符编码为UTF-8,确保正确显示中文等特殊字符。

  5. <title>Insert title here</title>: 设置页面的标题为"Insert title here"。你可以根据需要修改标题内容。

  6. <body>: 页面主体部分。

  7. <h1>Form</h1>: 在页面中显示一个标题为"Form"的一级标题。

  8. <form>: 表单标签,用于创建一个表单。

    • action="#": 表单提交的目标URL,这里设置为"#",表示表单将提交到当前页面。
    • th:action="@{/login}": 使用Thymeleaf的URL表达式,设置表单提交的目标URL为"/login",实际提交地址将由后端应用程序处理。
    • th:object="${loginBean}": 绑定表单数据的对象,这里是一个名为"loginBean"的对象,通常是一个Java Bean,其中的属性将在表单中使用。
    • method="post": 表单提交的HTTP方法,这里设置为"post",表示使用HTTP POST方法提交表单数据。
  9. <p>: 段落标签,用于包裹表单字段。

  10. <input>: 输入字段标签,用于创建表单输入字段。

    • type="text": 输入字段类型为文本。
    • th:field="*{uname}": 使用Thymeleaf的属性选择器,将输入字段绑定到名为"uname"的Java Bean属性,即loginBean.getUname()loginBean.setUname()
    • th:placeholder="请输入用户名": 输入字段的占位符,当用户未输入内容时,显示提示信息"请输入用户名"。
  11. <input type="submit" value="Submit"/>: 提交按钮,用户点击后将提交表单数据到后端应用程序。

  12. <input type="reset" value="Reset"/>: 重置按钮,用户点击后将清空表单中的数据。

总结:这段HTML代码创建了一个包含用户名和角色输入字段的表单,用户可以在输入框中输入相关信息,并点击"Submit"按钮提交表单数据到后端应用程序进行处理。在后端应用程序中,需要定义一个名为loginBean的Java Bean来接收表单数据。这个Java Bean需要有对应的unameurole属性,并提供相应的getter和setter方法。这样,当用户提交表单时,Thymeleaf会自动将表单数据绑定到loginBean的属性上,方便后续处理。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Spring MVC中,你可以使用@RequestParam注解来获取表单数据。同时,如果你使用Thymeleaf作为模板引擎,你可以在表单中使用Thymeleaf的语法来绑定表单数据。 首先,确保你已经在你的Spring MVC配置中配置了Thymeleaf视图解析器。例如,在你的`application.properties`或`application.yml`中添加以下配置: ``` spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 接下来,在你的Controller中,使用@RequestParam注解来获取表单数据。例如,假设你有一个表单提交了一个名为"name"的字段,你可以这样获取它的值: ```java @PostMapping("/submit") public String handleSubmit(@RequestParam("name") String name) { // 处理表单数据 return "redirect:/success"; } ``` 在上面的例子中,`@RequestParam("name")`表示获取名为"name"的表单字段的值,并将其赋值给`name`变量。 然后,在你的Thymeleaf模板中,你可以使用Thymeleaf的语法来绑定表单数据。例如,如果你想在表单中显示之前提交的名字,可以使用以下代码: ```html <form th:action="@{/submit}" method="post"> <input type="text" name="name" th:value="${name}" /> <button type="submit">Submit</button> </form> ``` 在上面的例子中,`${name}`表示从Controller传递过来的名为"name"的属性值。 这样,你就可以在Spring MVC中获取和使用Thymeleaf表单数据了。希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王摇摆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值