javascript提交表單,Springboot之Thymeleaf 表單提交

 2023-11-18 阅读 28 评论 0

摘要:@RequestMapping("/index") public String index(ModelMap map){//單個數據map.put("username", "入門案例");UserForm user = new UserForm();user.setPassword("test_ps");user.setUsername("test");map.put("userInf
@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;
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/178240.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息