gpt4 book ai didi

java - 如何修复 "java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ' 技能'可作为请求属性“错误

转载 作者:行者123 更新时间:2023-12-01 16:35:31 35 4
gpt4 key购买 nike

获取标题中看到的错误以及“执行处理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'时出错(模板:“profile” - 第35行,第48行)”,即下面的第4行html。如何解决这个问题?我查看了很多类似的问题,大多数都使用 @RequestMapping 而不是 @PostMapping 的方法。

@Controller
public class SkillController {

@Autowired
private SkillRepository skillRepository;

@Autowired
private AccountRepository accountRepository;

@GetMapping("/profile/{path}/skill")
public String view(@PathVariable String path, Model model) {
model.addAttribute("skill", new Skill());
return "redirect:/profile/" + path;
}

@PostMapping("/profile/{path}/skill")
public String addSkill(@PathVariable String path, @ModelAttribute("skill") Skill skill, BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
return "redirect:/profile/" + path;
}
try {
Account a = accountRepository.findByPath(path);
a.getSkills().add(skill);
accountRepository.save(a);
return "redirect:/profile/" + path;
} catch (DataIntegrityViolationException ex) {
bindingResult.rejectValue("name", "name", " Please try again!");
return "redirect:/profile/" + path;
}
}

}
<form th:action="@{/profile/{path}/skill(path=${path})}" th:object="${skill}" method="POST">
<table>
<tr>
<td><input type="text" th:field="*{name}"/></td>
<td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Character error.</td>
</tr>
<tr>
<td><button type="submit">Add skill</button></td>
</tr>
</table>
</form>
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Skill extends AbstractPersistable<Long> {

@NotEmpty
@Size(min = 4, max = 16)
@Column(unique = true)
private String name;

}

最佳答案

为了在重定向上传递属性,RedirectAttributes应该使用而不是 Model :

@GetMapping("/profile/{path}/skill")
public String view(@PathVariable String path, RedirectAttributes redirectAttrs) {
redirectAttrs.addFlashAttribute("skill", new Skill());
return "redirect:/profile/" + path;
}

关于java - 如何修复 "java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ' 技能'可作为请求属性“错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61957404/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com