Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsp Spring MVC-如何设置父div';当存在验证错误时,是否初始化?_Jsp_Spring Mvc_Jsp Tags - Fatal编程技术网

Jsp Spring MVC-如何设置父div';当存在验证错误时,是否初始化?

Jsp Spring MVC-如何设置父div';当存在验证错误时,是否初始化?,jsp,spring-mvc,jsp-tags,Jsp,Spring Mvc,Jsp Tags,有没有办法将任意元素的css类绑定到模型绑定状态 <form:form method="post" commandName="authForm" action="authenticate"> <div id="login-error" class="control-group"> <label>Login</label> <form:input path="name" /> <span class="h

有没有办法将任意元素的css类绑定到模型绑定状态

<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>

...

我在这里找不到合适的(通用的)解决方案。但是,您可以使用JavaScript并尝试查找视图中是否存在id为“name”的范围,并根据此条件可以实现您想要的结果

因为当指定的没有绑定错误时,它不会为相同的对象生成span标记。若“name”有错误,它将生成相应的带有id的span标记

希望这对你有帮助。
Cheeers.

我遇到了同样的问题,我使用jQuery解决了它

<div class="control-group">
    <form:label path="groupCode" cssClass="control-label"><spring:message code="lookUp.groupCode"/></form:label>
    <div class="controls">
        <form:input path="groupCode"/>
        <form:errors path="groupCode">
            <form:errors path="groupCode" cssClass="help-inline"/>
            <script type="text/javascript">
                $("#groupCode").parent().parent().addClass("error");
            </script>
        </form:errors>
    </div>
</div>

$(“#groupCode”).parent().parent().addClass(“错误”);

请您详细说明您的问题。我无法理解你的问题。这也可能是一个很好的解决方案。但据我所知,在jsp中混合java代码是非常肮脏的,不推荐这样做。对于紧急解决方案,这项工作仍然有效。您可以使用JSTL的三元运算符,而不是注入Java代码,例如:
${status.error?'error':'}