我正在使用Spring窗体库来处理我的应用程序中的搜索页面。这是从我的DD剪切显示的bean配置: bean name="/search.html" class="myapp.web.AccountSearchController" property name="sessionForm" value="true"/ propert
<bean name="/search.html" class="myapp.web.AccountSearchController"> <property name="sessionForm" value="true"/> <property name="commandName" value="searchAccounts"/> <property name="commandClass" value="myapp.service.AccountSearch"/> <property name="validator"> <bean class="myapp.service.AccountSearchValidator"/> </property> <property name="formView" value="accountSearch"/> <property name="successView" value="accountSearch"/> </bean>
验证器类很简单:
package myapp.service; import org.springframework.validation.Validator; import org.springframework.validation.Errors; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class AccountSearchValidator implements Validator { /** Logger for this class and subclasses */ protected final Log logger = LogFactory.getLog(getClass()); public boolean supports(Class clazz) { return AccountSearch.class.equals(clazz); } public void validate(Object obj, Errors errors) { AccountSearch accountSearch = (AccountSearch) obj; if (accountSearch == null) { errors.rejectValue("domainName", "error.accountSearch.neither-specified", null, "Value required."); } else if (accountSearch.getAccountId().isEmpty() && accountSearch.getDomainName().isEmpty() ) { errors.rejectValue("domainName", "error.accountSearch.neither-specified", null, "Value required."); } } }
在我的JSP中,我的表单显示在HTML表中。我想要将字段特定的错误作为单独的表行显示在相应的字段下。这是一个片段:
<tr> <td align="right" valign="top"><form:label path="domainName">Domain Name</form:label>:</td> <td><form:input path="domainName" size="30"/></td> </tr> <tr> <td> </td> <td><form:errors path="domainName" cssClass="error"/></td> </tr>
我的问题是 – 如何使错误行的输出以存在错误为条件?验证器实例是否可以从我的JSP访问? c:if标签的测试是什么?
谢谢,
-AJ
好的,我花了几天,但我想出了:<c:set var="domainNameErrors"><form:errors path="domainName"/></c:set> <c:if test="${not empty domainNameErrors}"> <tr> <td> </td> <td>${domainNameErrors}</td> </tr> </c:if>
这篇文章是非常有帮助的:
http://forum.springsource.org/archive/index.php/t-51044.html