如何访问Spring 3 MVC验证器结果在JSP中,而不使用表单taglib [英] How to access Spring 3 MVC validator results in JSP without using form taglib

查看:146
本文介绍了如何访问Spring 3 MVC验证器结果在JSP中,而不使用表单taglib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Spring 3 MVC表单,使用jsp taglibs。我需要根据表单中的一个字段是否有与之相关联的任何错误来添加一个类。这是我的HTML的一个狙击手:

 < div class =control-group error> <! - 这里:binding.hasErrors()? error: - > 
< form:label path =usernamecssClass =control-label>用户名< / form:label>
< div class =controls>
< form:input path =usernamecssClass =span3/>
< form:errors path =usernamecssClass =help-inline/>
< / div>
< / div>

所以在第一行,class属性有两个类control-group和error。我需要添加错误类,只有当该字段与它相关联的错误。我知道WebDataBinder是以某种方式包含在页面中,但是我不知道如何访问它。基本上我只是想执行一些好的旧式<%= binding.hasError()? error:%>,但是如何访问页面中的binder?

解决方案

有点更晦涩,我认为这更简单,因为它是一条线,这是什么,如果我只是使用像任何一个健全的Java开发应用的scriplets。 Taglib需要死去死去,然后再死。他们是可怕的,我不敢相信Java开发人员仍然认为他们实际上是帮助而不是浪费我们的时间。 PHP开发人员在使用这些东西时笑我们。

 < div class =control-group $ {requestScope ['org .springframework.validation.BindingResult.user']。hasFieldErrors('firstName')?'error':''}> 


I have a simple Spring 3 MVC form using jsp taglibs. 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:

<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". 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. Essentially I just want to execute some good old fashion <%= binding.hasError() ? "error" : "" %>, but how do I get access to the binder in the page?

解决方案

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. Taglibs need to die die die die, then die some more. They are horrible and I can't believe Java devs still think they actually help and not waste our utter time. PHP developers laugh at us when we use those things.

<div class="control-group ${requestScope['org.springframework.validation.BindingResult.user'].hasFieldErrors('firstName') ? 'error' : ''}">

这篇关于如何访问Spring 3 MVC验证器结果在JSP中,而不使用表单taglib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆