Java bean name';的BindingResult或普通目标对象;电话簿';可用作请求属性
我试图将验证添加到电话号码输入中,但在访问索引时我一直在解决这个错误 错误消息 原因:org.attoparser.ParseException:执行时出错 加工机 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (模板:“索引”-第76行,第73列) 原因:java.lang.IllegalStateException:既不是BindingResult也不是 bean名称“phonenumber”的普通目标对象可作为请求使用 属性 index.htmlJava bean name';的BindingResult或普通目标对象;电话簿';可用作请求属性,java,spring-boot,spring-data-jpa,thymeleaf,Java,Spring Boot,Spring Data Jpa,Thymeleaf,我试图将验证添加到电话号码输入中,但在访问索引时我一直在解决这个错误 错误消息 原因:org.attoparser.ParseException:执行时出错 加工机 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (模板:“索引”-第76行,第73列) 原因:java.lang.IllegalStateException:既不是BindingResult也不是 bean名称“phonenumber”的普通
<div class="myForm">
<form th:action="@{/save}" method="post" th:object="${phonebook}">
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Update or Create</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<input type="hidden" class="form-control" id="id" name="id" value=""/>
</div>
<div class="form-group">
<label for="surname" class="col-form-label">Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" value=""/>
</div>
<div class="form-group">
<label for="firstname" class="col-form-label">First Name:</label>
<input type="text" class="form-control" id="firstname" name="firstname" value=""/>
</div>
<div class="form-group">
<label for="phonenumber" class="col-form-label">Phone Number:</label>
<input type="text" class="form-control" th:field="*{phonenumber}" id="phonenumber" name="phonenumber" value=""/>
<span th:if="${#fields.hasErrors('phonenumber')}" class="help-block" th:errors="*{phonenumber}"></span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Save"/>
</div>
</div>
</div>
</div>
</form>
电话簿
@Entity
@Table(name = "Phonebook")
public class Phonebook {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@NotNull
@Size(max=15, message = "Max symbols is 15")
@Column(name = "phonenumber", length = 15, nullable = false)
private String phonenumber;
@Column(name = "surname", length = 50, nullable = false)
private String surname;
@Column(name = "firstname", length = 50, nullable = false)
private String firstname;
//getters and setter
我有两种方法来解决这个问题:—— 1.)使用@RequestParam获取电话簿对象,如下所示:—— 或者——使用@modeldattribute获取表单的值,如下所示:-- 1.)创建新的电话簿对象和外接程序模型属性:-
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public String showPage(Model model, @RequestParam(defaultValue = "0") int page) {
//your code
model.addAttribute("phoneBook", new Phonebook());
return "index";
}
2.)您的Thymeleaf/HTML页面中的更改(提交后使用th:object发送电话簿对象):--
4.)最后是使用getter和setter方法的Phonebook类。您在Phonebook中定义了phonenumber属性了吗class@GauravSrivastav是的,您可以检查代码您的解决方案修复了旧错误,但生成了一个新的org.attoparser.ParseException:(Line=52,Column=13)结构不完整:“您是否正确地用标记关闭了表单?@Ekera您在
”${phonebook}
@chelmertz之后缺少了一个“
”,但它会导致以前描述的错误above@Sumit是的,关门了
@PostMapping("/save")
public String save(@Valid @RequestParam("phonenumber")String phonenumber, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "redirect:/";
}else {
phonebookRepository.save(p);
}
return "redirect:/";
}
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public String showPage(Model model, @RequestParam(defaultValue = "0") int page) {
//your code
model.addAttribute("phoneBook", new Phonebook());
return "index";
}
<form th:action="@{/save}" method="post" th:object="${phoneBook}">
// your code
</form>
@PostMapping("/save")
public String save(@Valid @ModelAttribute("phoneBook")Phonebook p, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "redirect:/";
}else {
phonebookRepository.save(p);
}
return "redirect:/";
}