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

标签 java spring-boot thymeleaf

获取标题中看到的错误以及“执行处理器'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/

相关文章:

java - 如何注入(inject)spring boot的SpringApplication的mainApplicationClass?

java - Hibernate 无法使用 Spring Boot 在编译的 jar 中创建entityManagerFactory bean

spring-boot - 在类型 org.thymeleaf.expression.Dates 上找不到方法调用 : Method format(java. time.LocalDate,java.lang.String)

java - 如何从文件中检索数据

java - 我可以用 Java 编写类似 Notification 的东西吗?

Java 程序无缘无故地无限循环

java - 需要澄清引导 Spring Boot 应用程序的推荐方法

javascript - Thymeleaf 未转义的 JavaScript 内联

spring-boot - 如何使用 Thymeleaf 在 Spring Boot 中设置 Flash 消息

java - 枚举<?扩展界面>