Java spring在表单中显示错误的用户输入:input
我的控制器中有一个模型对象 比方说Java spring在表单中显示错误的用户输入:input,java,spring,Java,Spring,我的控制器中有一个模型对象 比方说 Class person{ Date birthDay; } 当它显示在表单:input中时,如果用户输入“aaaaa”,它当然会失败,我只需将用户引导到同一页面,并通过将用户再次放入模型来向他们显示错误。但是有没有办法让用户在输入字段中输入错误的内容 <form:input path="birthday"/> 将只显示为空,因为绑定失败。我是否必须使用DTO对象并将所有内容都设置为字符串,以便仍然可以显示错误的输入?我无法复制您的
Class person{
Date birthDay;
}
当它显示在表单:input中时,如果用户输入“aaaaa”,它当然会失败,我只需将用户引导到同一页面,并通过将用户再次放入模型来向他们显示错误。但是有没有办法让用户在输入字段中输入错误的内容
<form:input path="birthday"/>
将只显示为空,因为绑定失败。我是否必须使用DTO对象并将所有内容都设置为字符串,以便仍然可以显示错误的输入?我无法复制您的情况。你能提供更多的代码吗?Mine exapme显示错误输入:
public class Person {
private Date birthDay;
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public Date getBirthDay() {
return birthDay;
}
}
控制器:
@Controller
@RequestMapping("/person")
public class PersonController {
@RequestMapping(value ="/edit", method=RequestMethod.GET)
public String edit(Person person, BindingResult result){
return "person/edit";
}
@RequestMapping(value ="/edit", method=RequestMethod.POST)
public String submit(Person person, BindingResult result){
if(result.hasErrors()){
return "person/edit";
} else {
return "redirect:person/edit.ss";
}
}
Jsp:
出生日:
我无法复制您的情况。你能提供更多的代码吗?Mine exapme显示错误输入:
public class Person {
private Date birthDay;
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public Date getBirthDay() {
return birthDay;
}
}
控制器:
@Controller
@RequestMapping("/person")
public class PersonController {
@RequestMapping(value ="/edit", method=RequestMethod.GET)
public String edit(Person person, BindingResult result){
return "person/edit";
}
@RequestMapping(value ="/edit", method=RequestMethod.POST)
public String submit(Person person, BindingResult result){
if(result.hasErrors()){
return "person/edit";
} else {
return "redirect:person/edit.ss";
}
}
Jsp:
出生日:
什么是异常消息,您的声明是什么?您是否在@InitBinder方法中注册了日期编辑器(是sql还是util date)?通常您会在错误消息中显示“错误”输入。类似“AAAA不是有效日期”的内容异常消息是什么,您的声明是什么?您是否在@InitBinder方法中注册了日期编辑器(是sql还是util date)?通常您会在错误消息中显示“错误”输入。类似于“AAAA不是有效日期”