当在表单中输入输入时,我有以下实体应该被验证。我可以看到当有错误时,它只返回到相同的形式(这是预期的),但不会在实体中注释的get属性上显示错误消息。我有{< mvc:annotation />}
在我的disptacher servlet中
package com.web.portal.entity;
import java.io.Serializable;
import org.hibernate.validator.Pattern;
import org.hibernate.validator.Size;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Email;
@Entity
@Table(name = "faculty")
public class Faculty implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private byte[] image;
private String firstName;
private String lastName;
private String description;
private String email;
private String phone;
private String research;
private byte[] map;
@GeneratedValue
@Id
@Column(name = "id")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "image")
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
@NotBlank(message = "Field value should not be null")
@Column(name = "first_name")
@Pattern(regex = "[a-z-A-Z]*", message = "First name has invalid characters")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "last_name")
@NotBlank(message = "Field value should not be null")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "description")
@NotBlank(message = "Field value should not be null")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "email")
@NotBlank(message = "Field value should not be null")
@Email(message = "You provided an invalid email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Size(min = 10, max = 10, message = "Size should be ten : 4537653456")
@Column(name = "phone_number")
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Column(name = "research")
public String getResearch() {
return research;
}
public void setResearch(String research) {
this.research = research;
}
@Column(name = "location_map")
public byte[] getMap() {
return map;
}
public void setMap(byte[] map) {
this.map = map;
}
}
我的控制器正在关注:
package com.web.portal.controller;
import java.util.Map;
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.web.portal.entity.Faculty;
import com.web.portal.service.FacultyService;
@Controller
public class AdminManagerController {
private static final Logger LOG = Logger.getLogger(AdminManagerController.class);
@Autowired
FacultyService service;
public FacultyService getService() {
return service;
}
public void setService(FacultyService service) {
this.service = service;
}
@RequestMapping(value = "/manager.do")
public String facultyLists(Map<String, Object> map) {
Faculty faculty = new Faculty();
map.put("facultyList", service.getList(faculty));
return "faculty_and_staff";
}
@RequestMapping(value = "/fillInformation.do", method = RequestMethod.GET)
public String fillFaculty(ModelMap model) {
model.addAttribute("faculty", new Faculty());
return "add_faculty_info";
}
@RequestMapping(value = "/addFaculty.do", method = RequestMethod.POST)
public String addFaculty(@Valid Faculty faculty, BindingResult result, Map model) {
if (result.hasErrors()) {
return "redirect:/fillInformation.do";
} else {
service.addFaculty(faculty);
return "redirect:/manager.do";
}
}
}
我正在使用javax验证。我的jsp如下:
<body>
<div class="container">
<div class="login">
<h1>Add Faculty</h1>
<form:form method="POST" action="addFaculty.do" commandName="faculty">
<form:errors path="*" />
<table>
<tr>
<td><form:label path="image"></form:label>Picture</td>
<td><form:input path="image" placeholder="" accept="image/*" type="file"/></td>
</tr>
<tr>
<td><form:label path="firstName"></form:label> Firstname</td>
<td><form:input path="firstName" placeholder=""/></td>
答案 0 :(得分:0)
在你的Jsp中,你应该拥有
<form:error path="image"/>
。
当<form:input path="image" />
出错时,它会显示在<form:error>
标记中。
请遵循本教程:http://www.mkyong.com/spring-mvc/spring-mvc-form-errors-tag-example/
答案 1 :(得分:0)
我认为您应该将@NotBlank,@ Email等验证注释放在属性而不是其getter方法上。如果我错了,请忽略。
答案 2 :(得分:0)
问题在于“重定向”。验证错误消息被放置到请求属性,但没有请求属性将通过重定向
生存if (result.hasErrors()) {
return "redirect:/fillInformation.do";
}
请删除redirect:
并检查