在我的Spring启动项目中,将DTO对象传递到我的视图,然后在提交后将其发布。例如。发布表单后如何访问“ randomKey”?
@GetMapping("/{userId}/edit/feature/")
public String showEditFeature(@PathVariable("userId") Long userId, UserDto userDto,
Model model)
{
User user = userService.get(userId);
model.addAttribute("user", user);
model.addAttribute("randomKEy", "AnyObject");
return "user/edit/profile";
}
然后是我的帖子功能。
@PostMapping("profile_update")
public String watchFeatureUpdate(@Valid UserProfileDto UserProfileDto,
BindingResult result,
RedirectAttributes redirectAttributes, Model model)
{
Long userId = userService.updateUserProfile(userProfileDto);
redirectAttributes.addFlashAttribute("message", "Profile features updated
successfully!");
redirectAttributes.addFlashAttribute("alertClass", "alert-success");
return "redirect:/user/view/" + userId + "/profile";
}
您需要在HTML页面中包含“用户/编辑/配置文件”作为每个对象属性值的隐藏输入,以便在提交表单时将它们发送回去。
<input type="hidden" name="randomKEy" value="${randomKEy}">
请注意,即使在呈现的网页中看不到HTML内容,用户仍然可以操纵HTML内容。