java 如何在不使用表单标记库的情况下在 JSP 中访问 Spring 3 MVC 验证器结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12204649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:01:55  来源:igfitidea点击:

How to access Spring 3 MVC validator results in JSP without using form taglib

javajspdata-bindingspring-mvc

提问by chubbsondubs

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:

我有一个使用 jsp taglibs 的简单 Spring 3 MVC 表单。我需要根据表单中的字段是否有任何相关错误添加一个类。这是我的 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?

所以在第一行类属性有两个类“控制组”和“错误”。仅当该字段有与之关联的错误时,我才需要添加错误类。我知道 WebDataBinder 以某种方式包含在页面中,但我不知道如何访问它。基本上我只想执行一些好的旧时尚 <%= binding.hasError() ?"error" : "" %>,但是如何访问页面中的活页夹?

采纳答案by chubbsondubs

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.

虽然这有点晦涩,但我认为它更简单,因为它是一行,如果我只是像任何理智的 Java 开发人员那样使用脚本,就会如此。Taglibs需要死死死死,然后再死一些。他们太可怕了,我不敢相信 Java 开发人员仍然认为他们真的有帮助而不是浪费我们的全部时间。当我们使用这些东西时,PHP 开发人员会嘲笑我们。

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

回答by Xaerxess

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 errorsvariable (see Errorsinterface) bound to actual binding errors. You can check if field has errors via errors.hasFieldErrors(fieldName).

<spring:hasBindErrors>标签内部有一个errors变量(见Errors接口)绑定到实际的绑定错误。您可以通过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')......

回答by java_dude

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 : ''}