我在Spring Boot + Thymeleaf中创建了一个简单的CRUD,并且在应用程序上的所有其他CRUD工作得很好但是这个。当我提交表单时,该对象为控制器提供了空属性。
在下面,代码:
Artist.java
@Entity
public class Artist {
@Id
@GeneratedValue
private Long id;
@Column(nullable=false, length = 80)
private String artistName;
<getters and setters>
ArtistController.java
@Controller
public class ArtistController {
@Autowired
ArtistRepository artistRepository;
@GetMapping("/artist/add")
public String addArtist(Model model) {
model.addAttribute("artist", new Artist());
return "artist/form_artist";
}
@PostMapping("/artist/save")
public String artistSave(@ModelAttribute(name="artist") Artist artist) {
artistRepository.save(artist);
return "redirect:/artist";
}
}
现在,形式:
<form action="#" th:action="@{/artist/save}" th:object="${artist}" method="post" class="form-horizontal">
<div class="form-group">
<label for="id" >HobbyOn id:</label>
<input type="text" id="id" th:field="*{id}" readonly="readonly" class="form-control" placeholder="hobbyOn ID" />
</div>
<div class="form-group">
<label for="artistName" >Artist Name:</label>
<input type="text" th:field="*{artistName}" class="form-control" required="required" />
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Submit" />
</div>
</form>
我无法看到互联网和我自己的代码中的示例有什么不同。任何帮助都会受到欢迎。