有没有办法将任意元素的css类绑定到模型绑定状态? form:form method="post" commandName="authForm" action="authenticate" div id="login-error" class="control-group" labelLogin/label form:input path="name" / span class="help
<form:form method="post" commandName="authForm" action="authenticate">
<div id="login-error" class="control-group">
<label>Login</label>
<form:input path="name" />
<span class="help-inline"><form:errors path="name" /></span>
</div>
<div class="control-group">
<label>Password</label>
<form:input path="password" />
<span class="help-inline"><form:errors path="password" /></span>
</div>
<input type="submit" />
</form:form>
在这段代码中,我需要在没有错误时控制登录错误的类到控制组,并且当存在(第二个控制组的相同想法)时,需要控制组错误.
这里有什么常见的解决方案?
更新
这是我没有绑定错误时所需要的:
<div class="control-group"> <!-- !!!!!!!!!!!! --> <label>Login</label> <form:input path="name" /> <span class="help-inline"><form:errors path="name" /></span> </div>
这是绑定错误时我需要的:
<div class="control-group error"> <!-- !!!!!!!!!!!! --> <label>Login</label> <form:input path="name" /> <span class="help-inline"><form:errors path="name" /></span> </div>
寻找解决方案.
这是可行的解决方案,但我不确定它是否真的好主意:<%@taglib prefix="spring" uri="http://www.springframework.org/tags"%>
...
<form:form method="post" commandName="authForm" action="authenticate">
<spring:bind path="name">
<div class="control-group <%= status.isError() ? "error" : "" %>"
<label>Login</label>
<form:input path="name" />
<form:errors path="name" cssClass="help-inline" />
</div>
</spring:bind>
...
</form:form>
