本文章将介绍Thymeleaf中实现表单的语法和概念。

    编辑源代码以便将产品列表中的某个数据。已经将Product类的对象列表设置为具有变量名称product的上下文模型(参考:MyController.java中的实现)。

    如果要上机实践,请参考:Thymeleaf+SpringMVC5示例项目。这里不再重复创建项目的过程,这里将只介绍如何使用Thymeleaf Spring表达式和标签。

    这里创建一个Maven Web项目: thymeleaf-tutorials ,其目录结构如下所示 -

    1523793488142301.png

    数据访问类的实现:DAO.java -

    1. package com.e-learn.dao;
    2.  
    3. import java.math.BigDecimal;
    4. import java.sql.Timestamp;
    5. import java.text.ParseException;
    6. import java.text.SimpleDateFormat;
    7. import java.util.ArrayList;
    8. import java.util.Date;
    9. import java.util.List;
    10.  
    11. import com.e-learn.spring.bean.*;
    12.  
    13. /**
    14. * Mock persistence.
    15. */
    16. public class DAO {
    17.  
    18. private static final String NO_WEBSITE = null;
    19.  
    20. public static Product loadProduct() {
    21. try {
    22. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    23. return new Product("Wooden wardrobe with glass doors", Integer.valueOf(850), sdf.parse("2013-02-18"));
    24. } catch (ParseException ex) {
    25. throw new RuntimeException("Invalid date");
    26. }
    27. }
    28.  
    29. public static List<Product> loadAllProducts() {
    30. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    31. List<Product> products = new ArrayList<Product>();
    32. try {
    33. products.add(new Product("花生油", Integer.valueOf(125), sdf.parse("2018-02-18")));
    34. products.add(new Product("苏打饼干", Integer.valueOf(15), sdf.parse("208-02-15")));
    35. products.add(new Product("拿铁", Integer.valueOf(45), sdf.parse("2019-02-20")));
    36. products.add(new Product("调和油", Integer.valueOf(20), sdf.parse("2019-02-21")));
    37. products.add(new Product("大豆油", Integer.valueOf(49), sdf.parse("2019-02-15")));
    38. products.add(new Product("玉米汁", Integer.valueOf(80), sdf.parse("2019-02-17")));
    39. } catch (ParseException ex) {
    40. throw new RuntimeException("Invalid date");
    41. }
    42. return products;
    43. }
    44.  
    45. public static Timestamp loadReleaseDate() {
    46. try {
    47. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    48. Date date = sdf.parse("2014-01-31 15:00");
    49. return new Timestamp(date.getTime());
    50. } catch (ParseException ex) {
    51. throw new RuntimeException("Invalid date");
    52. }
    53. }
    54. }

    控制器类的实现:MyController.java -

    1. package com.e-learn.spring.controller;
    2.  
    3. import java.text.ParseException;
    4. import java.text.SimpleDateFormat;
    5. import java.util.ArrayList;
    6. import java.util.Date;
    7. import java.util.List;
    8.  
    9. import org.springframework.stereotype.Controller;
    10. import org.springframework.ui.Model;
    11. import org.springframework.web.bind.annotation.*;
    12. import org.springframework.web.bind.annotation.RequestMethod;
    13. import org.springframework.stereotype.Controller;
    14. import org.springframework.web.bind.annotation.RequestMapping;
    15. import org.springframework.web.bind.annotation.RequestMethod;
    16.  
    17. import com.e-learn.dao.DAO;
    18. import com.e-learn.spring.bean.Product;
    19.  
    20. @Controller
    21. public class MyController {
    22.  
    23. @GetMapping("/")
    24. public String index(Model model) throws ParseException {
    25. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    26. Product product = new Product("花生油", 129, sdf.parse("2018-02-18"), "");
    27. model.addAttribute("product", product);
    28. return "index";
    29. }
    30.  
    31. @RequestMapping(value = { "/addProduct" }, method = RequestMethod.GET)
    32. public String showaddProduct(Model model)throws ParseException {
    33. //List<Product> saleTypes = new ArrayList<Product>();
    34. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    35. Product product = new Product("花生油","花生油描述~", 119, sdf.parse("2018-02-11"), "CX");
    36. model.addAttribute("product", product);
    37.  
    38. return "addProduct";
    39. }
    40.  
    41. @RequestMapping(value = { "/saveProduct" }, method = RequestMethod.POST)
    42. public String saveprodcut(Model model, String name) throws ParseException {
    43. //List productList = DAO.loadAllProducts();
    44. //model.addAttribute("productList", productList);
    45. return "springel";
    46. }
    47.  
    48. }

    模板文件的实现:/webapp/WEB-INFO/views/addProduct.html -

    1. <html xmlns:th="http://www.thymeleaf.org">
    2. <head>
    3. <meta charset="UTF-8">
    4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    5. <link rel="stylesheet" th:href="@{/css/main.css}" />
    6. <title>Spring MVC5 + Thymeleaf 表单示例</title>
    7. </head>
    8. <body>
    9. <h2>Spring MVC5 + Thymeleaf 表单示例</h2>
    10. <form th:action="@{/saveProduct}" th:object="${product}" method="POST">
    11. <label for="firstName">产品名称:</label>
    12. <input type="text" th:field="*{name}" value="" /><br/>
    13.  
    14. <label for="firstName">有效日期:</label>
    15. <input type="text" th:field="*{availableFrom}" value="2018-03-28" /> <br/>
    16.  
    17. <label for="price">价格 (RMB):</label>
    18. <input type="text" th:field="*{price}" size="10" value="198" /><br/>
    19.  
    20. <input type="submit" value="提交"/>
    21. </form>
    22. </body>
    23. </html>

    运行上面项目,在浏览器中显示效果如下 -

    1523793491342216.png