使用Spring+Thymeleaf提交表单向控制器传递null对象问题求助
问题排查
问题说明
Spring搭配Thymeleaf开发时,表单提交后控制器拿到的Product对象属性全为null,完全参照教程实现仍无法正常运行,相关实现代码如下:
1. Model实体类
import java.math.BigDecimal; public class Product { public String name; public String description; public BigDecimal price; public Product(String name, String description, BigDecimal price) { this.name = name; this.description = description; this.price = price; } public Product() { } public String getName() { return name; } public String getDescription() { return description; } public BigDecimal getPrice() { return price; } }
2. DAO数据访问层
import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import com.example.sklepp.service.Product; import org.springframework.stereotype.Repository; @Repository public class ProductDao { private List<Product> products = Arrays.asList( new Product("Tea", "green", new BigDecimal("25.00")), new Product("Coffee", "black", new BigDecimal("30.00"))); public List<Product> all() { return products; } public void addProduct(Product product) { this.products.add(product); } public Product byName(String name) { for (Product product : products) { if (name.equals(product.getName())) { return product; } } return null; } }
3. Controller控制器代码
import com.example.sklepp.dao.ProductDao; import com.example.sklepp.service.Product; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("admin") public class AdminController { private final ProductDao productDao; public AdminController(ProductDao productDao) { this.productDao = productDao; } @GetMapping("/") public String admin(Model model) { model.addAttribute("newProduct", new Product()); return "admin/admin"; } @PostMapping("/add-product") public String addProduct(@ModelAttribute Product product) { System.out.println(product.getName()); return "redirect:/sklep"; } }
4. Thymeleaf模板代码
<html xmlns:th="http://www.thymeleaf.org" lang="en"> <head> <meta charset="UTF-8"> <title>Admin service</title> </head> <body> <h1>ADMIN</h1> <p>+++++++++++++++++++++</p> <form th:action="@{/admin/add-product}" th:object="${newProduct}" method="post"> <p>Adding new product: </p> <p>Name: <input type="text" th:field="*{name}"></p> <p>Price: <input type="text" th:field="*{price}"></p> <p>Description: <input type="text" th:field="*{description}"></p> <p><input type="submit" value="Add product"></p> </form> </body> </html>
修复方案
- 实体类补全setter方法:Spring MVC做表单参数绑定时需要通过setter方法给对象属性赋值,当前Product类仅实现了getter方法,参数无法写入所以返回全null值,需补充对应setter:
public void setName(String name) { this.name = name; } public void setDescription(String description) { this.description = description; } public void setPrice(BigDecimal price) { this.price = price; }
也可以引入Lombok依赖,给Product类加@Data注解自动生成通用方法,简化代码。
2. DAO层列表初始化逻辑修改:Arrays.asList()返回的是固定长度的列表,后续调用addProduct方法新增商品时会抛出UnsupportedOperationException异常,建议改为ArrayList初始化:
private List<Product> products = new ArrayList<>(Arrays.asList( new Product("Tea", "green", new BigDecimal("25.00")), new Product("Coffee", "black", new BigDecimal("30.00"))));
内容的提问来源于stack exchange,提问作者Daniel
相关产品推荐
相关产品推荐

