2014-03-13 2 views
1

양식에 입력을 입력 할 때 유효성이 검사되는 다음 엔티티가 있습니다. 내가 오류가있을 때 볼 수 있습니다, 단순히 의도 한 동일한 양식으로 반환되지만 엔터티에 주석이 속성을 가져올 때 오류 메시지를 표시하지 않습니다. 나는 {< MVC를 : 주석 />}이jsr-303 jsp에서 엔티티에 대한 유효성 검증 오류가 표시되지 않습니다.

내 disptacher 서블릿

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의 유효성 검사를 사용하고 있습니다.

<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

당신의 JSP로, 당신은 <form:error path="image"/>이 있어야합니다 내 JSP 따를 수 있습니다. <form:input path="image" />에 오류가있는 경우 <form:error> 태그에 표시됩니다. 만약 내가 잘못 무시 .. http://www.mkyong.com/spring-mvc/spring-mvc-form-errors-tag-example/

+0

감사를 확인하시기 바랍니다 더 요청 리디렉션으로 살아남을 것입니다 속성이 없습니다. 내 JSP를 보면 나는 와일드 카드

를 가지고있다. 경로의 모든 오류를 포착해야합니다. – user3414115

+0

아, 내 잘못이야. 컨트롤러 메소드의 매개 변수에 Model 모델이 있으면 jsp로 다시 리디렉션하기 전에 model.addAttribute ("faculty", faculty)를 사용 하시겠습니까? – CodeBender

+0

이상한. 테스트 프로젝트를 만들고 코드를 사용해 보았습니다. 유효성 검사가 작동합니다. – CodeBender

0

나는 당신이 @의 NotBlank을 배치해야한다고 생각하는 대신 자신의 getter 메소드의 속성을 통해 이메일 등 검증 주석 @

이 자습서를 따르십시오.

0

문제는 "리디렉션"입니다. 검증 오류 메시지 특성을 요청하기 위해 배치되지만

if (result.hasErrors()) { 
    return "redirect:/fillInformation.do"; 
} 

redirect:을 제거하고 응답 CodeBender에 대한