我正在使用表单标签.
<form:form commandName="foo">
<div class="myclass ">
<label>Foo</label>
<form:input path="fooName"/>
</div>
<div class="controls">
<input type="submit" class="btn" value="Submit"/>
</div>
</form:form>
Run Code Online (Sandbox Code Playgroud)
题
有没有办法找出特定字段上是否发生错误?
我知道<form:erros path="fooName"/>但这会打印出错误信息.我根据fooName属性是否发生错误而返回true或false .我需要这个,因为如果错误发生,那么我可以在error旁边插入css类my class
Sla*_*hin 16
对的,这是可能的:
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<form:form commandName="foo">
<spring:bind path="fooName">
<div class="myclass ${status.error ? 'error' : ''}">
<label>Foo</label>
<form:input path="fooName"/>
</div>
</spring:bind>
<div class="controls">
<input type="submit" class="btn" value="Submit"/>
</div>
</form:form>
Run Code Online (Sandbox Code Playgroud)
将字段封装在<spring:bind>标记内部时,您可以访问status类型的隐式变量BindStatus.您可以使用它来检查该字段是否有错误.
您还可以找到有用的以下链接:
这是另一种方法<spring:hasBindErrors>(在其中你可以访问errors类型的变量Errors),它只能在JSP 2.2的环境中工作:
<spring:hasBindErrors name="foo">
<div class="myclass ${errors.hasFieldErrors('fooName') ? 'error' : ''}">
<label>Foo</label>
<form:input path="fooName"/>
</div>
</spring:hasBindErrors>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5512 次 |
| 最近记录: |