2016-06-27 68 views
-1

我使用彈簧窗體驗證來驗證我的窗體輸入字段..我遇到了BindingResult問題,即使我的輸入字段爲空/空,result.hasErrors()始終返回false。 。我管理的所有進口,正如我所說的一切期待罰款me.But,我的驗證工作不和需求與修復這個幫助...春天窗體驗證BindingResult總是返回false

形式:

<%@include file="././Header.jsp"%> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<script src="<c:url value="/js/smlibray/emailCommunicate.js"/>"></script> 
<link href="<c:url value="/js/jquery-ui.css"/>" rel="stylesheet" /> 
<script src="<c:url value="/js/jquery-ui.js"/>"></script> 

<input type="hidden" name="applicationUrl" id="applicationUrl" 
    value="${pageContext.request.contextPath}"> 

<div class="container sec-container middle-sec"> 
    <div class="form-fields row"> 
     <div class="col-md-12 col-sm-12 col-xs-12"> 
      <form:form class="form-horizontal policy-form" id="emailCommu-form" method="POST" action="sendEmailMessage" commandName="emailForm"> 
       <div class="form-group"> 
       <h1 class="email-header">SiteMidner Email Notification</h1> 
       <c:choose> 
        <c:when test="${not empty sMsg}"> 
         <div class="email-Commu-SMsg">* ${sMsg}</div> 
        </c:when> 
        <c:otherwise> 
         <c:if test="${not empty eMsg}"> 
          <div class="email-Commu-EMsg">* ${eMsg}</div> 
         </c:if> 
        </c:otherwise> 
       </c:choose> 
       <div class="form-emailCommu"> 
        <label for="emailSub-txtArea" class="email-ContentHead">Enter 
         the Subject Line for the Email : </label> 
        <div class="email-sm-textDiv"> 
         <form:textarea path="emailSubject" name="emailSubject" id="emailSubTxtArea" 
          placeholder="Email Subject"/> 
        </div> 
        <span><form:errors path="emailSubject" cssClass="error" /></span> 

       </div> 

       <div class="form-emailCommu"> 
        <label for="emailBod-txtArea" class="email-ContentHead">Enter 
         the Message Body for the Email : </label> 
        <div class="email-sm-textDiv"> 
         <form:textarea path="emailMsg" name="emailMsg" id="emailBodyTxtArea" 
          placeholder="Email Body"/> 
        </div> 
        <span><form:errors path="emailMsg" cssClass="error" /></span> 

       </div> 

       <div class="email-sendButton"> 
        <input type="submit" class="styled-button" value="Send Email"></input> 
       </div> 
       </div> 
      </form:form> 

     </div> 
     <!--form-fields close --> 
    </div> 
</div> 

控制器代碼:

@RequestMapping(value="/sendEmailMessage",method=RequestMethod.POST) 
public ModelAndView sendEmailCommunication(@Valid @ModelAttribute("emailForm") EmailReqInfo emailInfo,BindingResult result,HttpServletRequest request){ 
    ModelAndView view = null; 
    StringBuffer sMsg = new StringBuffer(); 
    StringBuffer eMsg = new StringBuffer(); 
    boolean isAdmin = false; 
    try{ 
    String loggedInUser = request.getHeader("sm_user").trim(); 
    isAdmin = getUserAdminRights(request); 

    if(result.hasErrors()){ 
     view = new ModelAndView("EmailCommunication"); 
     view.addObject("isAdmin", isAdmin); 
     return view; 
    } 
    else{ 
     String emailSubject = emailInfo.getEmailSubject(); 
     String emailMsg = emailInfo.getEmailMsg().replace("\n", "<br />\n"); 
    boolean status = emailService.sendEmailCommuncation(emailSubject,emailMsg); 
    if(status){ 
     sMsg.append(" Sending SiteMinder Notification Email was Success."); 
    } 
    else{ 
     eMsg.append(" Oops! Something went wrong while sending Email Notification. Pls check logs"); 
    } 
    } 
    } 
    catch(Exception ex){ 
     ex.printStackTrace(); 
     eMsg.append("Oops! Something went wrong while sending Email Notification. Pls check logs"); 
    } 
    view = new ModelAndView("EmailCommunication"); 
    view.addObject("emailForm", new EmailReqInfo()); 
    view.addObject("isAdmin", isAdmin); 
    view.addObject("sMsg", sMsg.toString()); 
    view.addObject("eMsg", eMsg.toString()); 
    return view; 
} 

電子郵件型號:

import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 

import org.hibernate.validator.constraints.NotEmpty; 

public class EmailReqInfo 
{ 

    @NotNull @NotEmpty 
    @Size(min=1) 
    private String emailSubject; 
    @NotNull @NotEmpty 
    private String emailMsg; 
    public String getEmailSubject() { 
     return emailSubject; 
    } 
    public void setEmailSubject(String emailSubject) { 
     this.emailSubject = emailSubject; 
    } 
    public String getEmailMsg() { 
     return emailMsg; 
    } 
    public void setEmailMsg(String emailMsg) { 
     this.emailMsg = emailMsg; 
    } 
} 

我有必要的註釋在我的彈簧mvc.xml在固定validation..thanks

+0

什麼是領域的錯誤是你在綁定結果?你能發帖 – Saravana

+0

HI Sarvana,我沒有得到任何錯誤,即使我發送空輸入到模型,result.hasErrors()總是返回false,所以它沒有進入if條件..我沒有看到任何錯誤..任何想法,我錯了.. –

回答

2

@NotNull需要

<mvc:annotation-driven> 
    <mvc:message-converters register-defaults="false"> 
    <bean id="jacksonMessageConverter" 
     class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> 
    </mvc:message-converters> 
    </mvc:annotation-driven> 

幫助:檢查值是否不爲空,忽略內容。

@NotEmpty:檢查值是否不爲空也不爲空。如果它只有空的空間,它將允許它不空。

@NotBlank:檢查值是否不爲空也不爲空,先修剪值。這意味着,它不會允許只是空的空間

所以,如果你想驗證一個字段不是空的,但它也沒有隻是空的空間,但文本,你應該使用@NotBlank 。

這個例子是工作,嘗試做相同的:

實體:

@Entity 
@Table(name="EMPLOYEE") 
public class Employee { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int id; 

    @Size(min=3, max=50) 
    @Column(name = "NAME", nullable = false) 
    private String name; 

    @NotNull 
    @DateTimeFormat(pattern="dd/MM/yyyy") 
    @Column(name = "JOINING_DATE", nullable = false) 
    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate") 
    private LocalDate joiningDate; 

    @NotNull 
    @Digits(integer=8, fraction=2) 
    @Column(name = "SALARY", nullable = false) 
    private BigDecimal salary; 

    @NotEmpty 
    @Column(name = "SSN", unique=true, nullable = false) 
    private String ssn; 
//getter & setter 
} 

控制器:

@RequestMapping(value = { "/new" }, method = RequestMethod.POST) 
    public String saveEmployee(@Valid Employee employee, BindingResult result, 
      ModelMap model) { 

     if (result.hasErrors()) { 
      return "registration"; 
     } 


     service.saveEmployee(employee); 

     model.addAttribute("success", "Employee " + employee.getName() + " registered successfully"); 
     return "success"; 
    } 

registation.jsp

<h2>Registration Form</h2> 

    <form:form method="POST" modelAttribute="employee"> 
     <form:input type="hidden" path="id" id="id"/> 
     <table> 
      <tr> 
       <td><label for="name">Name: </label> </td> 
       <td><form:input path="name" id="name"/></td> 
       <td><form:errors path="name" cssClass="error"/></td> 
      </tr> 

      <tr> 
       <td><label for="joiningDate">Joining Date: </label> </td> 
       <td><form:input path="joiningDate" id="joiningDate"/></td> 
       <td><form:errors path="joiningDate" cssClass="error"/></td> 
      </tr> 

      <tr> 
       <td><label for="salary">Salary: </label> </td> 
       <td><form:input path="salary" id="salary"/></td> 
       <td><form:errors path="salary" cssClass="error"/></td> 
      </tr> 

      <tr> 
       <td><label for="ssn">SSN: </label> </td> 
       <td><form:input path="ssn" id="ssn"/></td> 
       <td><form:errors path="ssn" cssClass="error"/></td> 
      </tr> 

      <tr> 
       <td colspan="3"> 
        <c:choose> 
         <c:when test="${edit}"> 
          <input type="submit" value="Update"/> 
         </c:when> 
         <c:otherwise> 
          <input type="submit" value="Register"/> 
         </c:otherwise> 
        </c:choose> 
       </td> 
      </tr> 
     </table> 
    </form:form> 
0

我看到Hibernate Validators的問題。 @NotEmpty無論如何都是在@NotNull之上。

嘗試從表單中刪除@NotEmpty驗證約束並再次執行。