您好我正在使用spring boot 1.5.9.RELEASE并添加了spring-webmvc(4.3.13.RELEASE)依赖项。
当我点击带有某些表单数据的提交按钮时,它会进入服务器并检查是否存在某些错误,并将其打印在控制台上,然后返回到表单输入页面。但是没有打印错误信息。
我使用ModelAttribute对象提交表单,对象定义如下:
@Entity
@Table(name="MEMBER", uniqueConstraints={
@UniqueConstraint(columnNames={"USER_SEQ"}),
@UniqueConstraint(columnNames={"USER_EMAIL"}),
@UniqueConstraint(columnNames={"NICKNAME"})
})
@Data
@NoArgsConstructor
@RequiredArgsConstructor(staticName="of")
public class Member implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="USER_SEQ", unique=true, nullable=false)
private Long userSeq;
@Email
@NotNull
@NonNull
@Column(name="USER_EMAIL", unique=true, nullable=false, length=50)
private String userEmail;
@NotNull
@NonNull
@Column(name="USER_PW", nullable=false, length=255)
private String userPw;
@NotNull
@Size(min=2, max=10)
@NonNull
@Column(name="NICKNAME", unique=true, nullable=false, length=20)
private String nickname;
@OneToOne(mappedBy="member", cascade=CascadeType.ALL)
private MemberSecurity security;
@Override
public String toString() {
return "Member [userSeq=" + userSeq + ", userEmail=" + userEmail + ", userPw=" + userPw + ", nickname="
+ nickname + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Member other = (Member) obj;
if (userSeq == null) {
if (other.userSeq != null)
return false;
} else if (!userSeq.equals(other.userSeq))
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((userSeq == null) ? 0 : userSeq.hashCode());
return result;
}
}
在提交通过的控制器中,我编码如下:
@RequestMapping(value="/join", method=RequestMethod.POST)
public String join(Model model, @Valid @ModelAttribute("join") Member member, BindingResult bindingResult, RedirectAttributes redirect){
if(bindingResult.hasErrors()){
viewNameChooser.joinForm(model);
System.err.println("ERROR COUNT: " + bindingResult.getErrorCount());
System.err.println("ERROR FIELD: " + bindingResult.getFieldError().getField());
System.err.println("ERROR CODE: " + bindingResult.getFieldError().getCode());
return go();
}
redirect.addFlashAttribute("joinSuccess", String.valueOf(memberService.save(member)) );
return "redirect:/member/login-form";
}
,控制台输出为:
ERROR COUNT: 1
ERROR FIELD: nickname
ERROR CODE: Size
现在我在classpath中注册了自定义错误消息:META-INF / messages.properties:
Email.member.userEmail=It is not email type text.
NotNull.member.userEmail=Please, type your email id.
NotNull.member.userPw=Please, type your password.
NotNull.member.nickname=Please, type your nickname.
Size.member.nickname=The length of nickname must be greater than or equal 2 and less than or equal 10.
到目前为止一切顺利,但在我的freemarker模板中,没有显示错误消息。我编码如下:
<#assign form=JspTaglibs["http://www.springframework.org/tags/form"] />
<form id="join-form" action="<@spring.url '/member/join' />" method="post" style="width:600px; margin:0 auto;">
<@spring.bind "join" />
<#-- skipped -->
<@form.errors "*" />
<#-- skipped -->
</form>
&lt; @ form.errors&#34; *&#34; /&GT;
应该包含错误消息,但不是。我在哪里错过了?请帮帮我。
编辑:我修改了控制器方法。
@RequestMapping(value="/join", method=RequestMethod.POST)
public ModelAndView join(@Valid @ModelAttribute("join") Member member, BindingResult bindingResult, RedirectAttributes redirect){
if(bindingResult.hasErrors()){
//viewNameChooser.joinForm(bindingResult);
ModelAndView modelAndView = new ModelAndView("template", bindingResult.getModel());
modelAndView.addObject("viewName", "joinForm");;
System.err.println("ERROR COUNT: " + bindingResult.getErrorCount());
System.err.println("ERROR FIELD: " + bindingResult.getFieldError().getField());
System.err.println("ERROR CODE: " + bindingResult.getFieldError().getCode());
return modelAndView;
}
ModelAndView modelAndView = new ModelAndView("redirect:/member/login-form");
redirect.addFlashAttribute("joinSuccess", String.valueOf(memberService.save(member)) );
return modelAndView;
}
现在我正在使用ModelAndView。