首页 > 解决方案 > 我怎样才能正确地从表单中获取数据

问题描述

例如,我有实体用户

我应该在 data-th-object 中使用什么类以获得良好的实践:它应该是 User 或者 RegistrationForm 或 UserDTO 什么是从表单中获取数据的最佳解决方案?

<form method=POST data-th-object=${what is good practice for use there} data-th-action=@{/registration}>
   //some data
</form>

标签: javahtmlformspostthymeleaf

解决方案


我个人喜欢使用专用的表单数据对象,而不是表单中的实体本身,以便更清晰地分离关注点。我在https://www.wimdeblauwe.com/blog/2021/05/23/form-handling-with-thymeleaf/上更详细地解释了这一点


推荐阅读