Springboot之Thymeleaf 表单提交

@RequestMapping("/index")
public String index(ModelMap map){
    //单个数据
    map.put("username", "入门案例");

    UserForm user = new UserForm();
    user.setPassword("test_ps");
    user.setUsername("test");

    map.put("userInfo", user);
    return "admin/index";
}

通过${}和*{}的方式来读取表单类对象

<!DOCTYPE html>
<!-- 需要添加
<html  xmlns:th="http://www.thymeleaf.org">
这样在后面的th标签就不会报错
 -->
<html  xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title th:text="${username}">xx</title>
</head>
<body>
<h1 th:text="${username}">Hello World</h1>
<h1>获取对象信息</h1>
<h2>1、通过直接访问对象的方式</h2>
<p th:text="${userInfo.username}"></p>
<p th:text="${userInfo.password}"></p>


<h2>2、通过th:object访问对象的方式</h2>
<div th:object="${userInfo}">
    <p th:text="*{username}"></p>
    <p th:text="*{password}"></p>
</div>

<h1>表单提交</h1>
<!-- 表单提交用户信息,注意字段的设置,直接是*{} -->
 <form action="#" th:action="@{/add}" th:object="${userInfo}" method="post">  
  <input type="text" th:field="*{username}" />  
  <input type="text" th:field="*{password}" />  
  <input type="submit" />  
</form> 
</body>
</html>

@ResponseBody
@RequestMapping(value="/add",method=RequestMethod.POST)
public String add(@ModelAttribute UserForm user){
    String username = user.getUsername();
    String password = user.getPassword();
    return username+"__"+password;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用Spring Boot和Thymeleaf来实现邮箱验证登录功能。下面是一些基本的步骤: 1. 添加所需的依赖:在您的项目的pom.xml文件中添加Spring Boot和Thymeleaf的依赖项。 ```xml <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> ``` 2. 创建一个包含登录表单的HTML页面。您可以使用Thymeleaf模板引擎来渲染页面和处理表单提交。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form action="/login" method="post"> <input type="email" name="email" placeholder="Email" required/> <input type="password" name="password" placeholder="Password" required/> <button type="submit">Login</button> </form> </body> </html> ``` 3. 创建一个控制器来处理登录请求和验证逻辑。 ```java @Controller public class LoginController { @GetMapping("/login") public String showLoginForm() { return "login"; } @PostMapping("/login") public String login(@RequestParam("email") String email, @RequestParam("password") String password) { // 进行邮箱验证和登录逻辑的处理 // ... return "redirect:/home"; } @GetMapping("/home") public String home() { return "home"; } } ``` 4. 创建一个用于显示登录成功页面的HTML模板。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1>Welcome!</h1> </body> </html> ``` 这只是一个基本的示例,您可能还需要添加更多的逻辑来实现完整的邮箱验证和登录功能。您可以使用Spring Security来处理用户认证和授权,以及使用Java Mail API来发送验证邮件。希望这个简单的示例能帮助您入门。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

讓丄帝愛伱

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

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

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

打赏作者

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

抵扣说明:

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

余额充值