[英]How to access Spring 3 MVC validator results in JSP without using form taglib
I have a simple Spring 3 MVC form using jsp taglibs. 我有一个使用jsp taglibs的简单的Spring 3 MVC表单。 I need to add a class based on whether a field within the form has any errors associated with it or not.
我需要根据表单中的字段是否有任何与之关联的错误来添加类。 Here is a snipet of my HTML:
这是我的HTML的snipet:
<div class="control-group error"> <!-- HERE: binding.hasErrors() ? "error" : "" -->
<form:label path="username" cssClass="control-label">User Name</form:label>
<div class="controls">
<form:input path="username" cssClass="span3"/>
<form:errors path="username" cssClass="help-inline" />
</div>
</div>
So on the first line the class attribute has two classes "control-group" and "error". 所以在第一行,class属性有两个类“control-group”和“error”。 I need to add error class only if that field has an error associated with it.
我只需要在该字段有与之关联的错误时添加错误类。 I know the WebDataBinder is included in the page somehow, but I don't know how to access it.
我知道WebDataBinder以某种方式包含在页面中,但我不知道如何访问它。 Essentially I just want to execute some good old fashion <%= binding.hasError() ?
基本上我只是想执行一些好的旧时尚<%= binding.hasError()? "error" : "" %>, but how do I get access to the binder in the page?
“error”:“”%>,但如何访问页面中的活页夹?
Did you try <spring:hasBindErrors>
tag (I don't understand what you mean writing "without using form taglib")? 您是否尝试过
<spring:hasBindErrors>
标签(我不明白您的意思是“不使用表单taglib”)?
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<spring:hasBindErrors name="yourCommandName">
<c:if test="${errors.hasFieldErrors('username')}">
<c:set var="errorClass" value="error" />
</c:if>
</spring:hasBindErrors>
<div class="control-group <c:out value='${errorClass}' />">
Edit after comments: 评论后编辑:
Inside <spring:hasBindErrors>
tag there is errors
variable (see Errors
interface ) bound to actual binding errors. 在
<spring:hasBindErrors>
标记内部,存在绑定到实际绑定错误的errors
变量(请参阅Errors
接口 )。 You can check if field has errors via errors.hasFieldErrors(fieldName)
. 您可以通过
errors.hasFieldErrors(fieldName)
检查字段是否有错误。
And really obscure way to get field errors without any tag is requestScope['org.springframework.validation.BindingResult.yourCommandName'].hasFieldErrors('username')
... requestScope['org.springframework.validation.BindingResult.yourCommandName'].hasFieldErrors('username')
是一个非常模糊的方法来获取没有任何标记的字段错误requestScope['org.springframework.validation.BindingResult.yourCommandName'].hasFieldErrors('username')
...
While this is a little more obscure I think it's simpler because it's a single line which is what it would be if I were just using scriplets like any sane Java dev should. 虽然这有点模糊,但我觉得它更简单,因为它是一条单行,如果我只是像任何理智的Java开发人员那样使用scriplet就会是这样。 Taglibs need to die die die die, then die some more.
Taglib需要死死模具,然后再死一些。 They are horrible and I can't believe Java devs still think they actually help and not waste our utter time.
它们非常可怕,我无法相信Java开发人员仍然认为他们确实有所帮助,而不是浪费我们的全部时间。 PHP developers laugh at us when we use those things.
PHP开发人员在我们使用这些东西时嘲笑我们。
<div class="control-group ${requestScope['org.springframework.validation.BindingResult.user'].hasFieldErrors('firstName') ? 'error' : ''}">
There is a better way to get the error message 有一种更好的方法来获取错误消息
<spring:hasBindErrors name="yourCommandName">
${errors.hasFieldErrors('userId') ? errors.getFieldError('userId').defaultMessage : ''}
</spring:hasBindErrors>
And one liner 还有一个班轮
${requestScope['org.springframework.validation.BindingResult.user'].hasFieldErrors('emailId') ? requestScope['org.springframework.validation.BindingResult.user'].getFieldError('emailId').defaultMessage : ''}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.