我使用彈簧窗體驗證來驗證我的窗體輸入字段..我遇到了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
什麼是領域的錯誤是你在綁定結果?你能發帖 – Saravana
HI Sarvana,我沒有得到任何錯誤,即使我發送空輸入到模型,result.hasErrors()總是返回false,所以它沒有進入if條件..我沒有看到任何錯誤..任何想法,我錯了.. –