我试图根据当前页面验证期间导致的错误来了解是否可以在页面上显示某些HTML元素。
例如
我有一个隐藏了文本框的表单。
当用户提交表单并且表单有任何错误时,当用户看到包含错误信息的页面时,他们应该会看到隐藏的文本框。
试图使用伪代码来解释我在下面尝试。
通过Spring验证设置错误,如下所示:
在验证器类中,我有类似的东西,
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "mainForm", "mainform.error.display.alternateText",
"error occured");
在JSP中,我有类似的东西,
<td id="alternateTextBox"
<c:choose>
<c:when test="${not empty mainForm.error}">
style="width:50%;visibility:visible" >
</c:when>
<c:otherwise>
style="width:50%;visibility:hidden" >
</c:otherwise>
</c:choose>
<s:message code="standing_instructions.RELATIONSHIP" />
<form:input path="alternateText" id="alternateText" size="50"/>
</td>
最佳答案
可以的
尝试像:
在验证器中添加字段错误:
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "mainForm", "mainform.error.display.alternateText", "mainForm required!");
在jsp中,您可以在表单内部显示如下错误:
<c:set var="er"><form:errors path="mainForm"/></c:set>
<c:if test="${not empty er}">
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button> <h4>Validation Error :</h4>
Please correct the following:<br/>
${er}
</div>
</c:if>