0
我遇到了顯示錯誤消息的問題。我使用Spring MVC併發送類似對象UserForm的表單,我想使用Hibernate驗證器,這是工作,但我看不到任何錯誤消息。Hibernate Validator的Spring MVC不顯示messages.properties中的錯誤
這是形式回到Home.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="./resources/styles/style.css" media="screen" />
<title>Home</title>
</head>
<body>
<h1>
User Form
</h1>
<form:form action="addUser" commandName="userform" method="get">
<table>
<tr>
<td>Name: </td>
<td><form:input path="name"/></td>
<td><form:errors path="name" cssClass="error"/></td>
</tr>
<tr>
<td>Text: </td>
<td><form:textarea path="text"/></td>
<td><form:errors path="text" cssClass="error"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Send"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
有是處理上述
package cz.solution.controllers;
import java.util.Date;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import cz.morosystems.form.UserForm;
import cz.morosystems.tools.JobSheduler;
@Controller
public class UserController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String showMyform(Model model) {
model.addAttribute("userform", new UserForm());
return "home";
}
@RequestMapping(value = "/addUser")
public String showFilledForm(@Valid @ModelAttribute("usermessage") UserForm userform, BindingResult result,
Model model) {
userform.setDate(new Date());
model.addAttribute("userform", userform);
if(result.hasErrors()){
return "/home";
}else{
return "displayUser";
}
}
}
用戶窗體形式針對home.jsp處理表單
package cz.solution.form;
import java.util.Date;
import org.hibernate.validator.constraints.NotEmpty;
public class UserForm {
@NotEmpty
private String name;
@NotEmpty
private String text;
private Date date;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
我說我的控制器UserController中this to servlet-context.xml
<beans:bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<beans:property name="basename" value="messages" />
</beans:bean>
我messages.properties文件夾中的src/main /資源
NotEmpty.userForm.name = Name is required!
NotEmpty.userForm.text = Text is required!
當一個輸入的是空的,這一次的演出形式,但沒有任何錯誤消息。我嘗試添加到UserForm
@NotEmpty(message="Name is required")
private String name;
但是沒有再次成功。感謝您的所有提示。