form:错误标记存在于jsp文件中,但在页面源代码中消失

时间:2015-03-01 15:10:21

标签: spring twitter-bootstrap jsp spring-mvc

我使用spring mvc 2.5.6,bootstrap3.3.2,jsp文件是:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="springForm" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <%--<link rel="icon" href="../../favicon.ico">--%>

    <title>Device check</title>
    <!-- Bootstrap core CSS -->
    <link href="${pageContext.request.contextPath}/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="${pageContext.request.contextPath}/resources/bootstrap/css/device.css" rel="stylesheet">

    <style>
        .error {
            color: #ee0d25;
        }
    </style>

</head>
    <body>

            <form class="form-device" method="POST" command="queryForm">
                <h2 class="form-device-heading">Please fill the form</h2>

                <label for="deviceId" class="sr-only">deviceId</label>
                <input type="text" name="deviceId" id="deviceId" class="form-control" placeholder="deviceId">
                <td><form:errors path="deviceId" cssClass="error" /></td>

                <label for="startTime" class="sr-only">startTime</label>
                <input type="text" name="startTime" id="startTime" class="form-control" placeholder="startTime">
                <td><form:errors path="startTime" cssClass="error" /></td>


                <label for="endTime" class="sr-only">endTime</label>
                <input type="text" name="endTime" id="endTime" class="form-control" placeholder="endTime">
                <td><form:errors path="endTime" cssClass="error" /></td>


                <label for="environment" class="sr-only">environmnet</label>
                <input type="text" name="environment" id="environment" class="form-control"
                       placeholder="daily or online">
                <td><form:errors path="environment" cssClass="error" /></td>

                <div class="checkbox">
                </div>
                <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
            </form>

    </body>
</html>

这里有四个输入框,用spring的验证器验证它们。但是当我访问localhost:8080 / form.htm时,网页的源代码如下:

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Device check</title>

    <!-- Bootstrap core CSS -->
    <link href="/wsf-ops/resources/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="/wsf-ops/resources/bootstrap/css/device.css" rel="stylesheet">

    <style>
        .error {
            color: #ee0d25;
        }
    </style>

</head>
    <body>

            <form class="form-device" method="POST" command="queryForm">
                <h2 class="form-device-heading">Please fill the form</h2>

                <label for="deviceId" class="sr-only">deviceId</label>
                <input type="text" name="deviceId" id="deviceId" class="form-control" placeholder="deviceId">
                <td></td>

                <label for="startTime" class="sr-only">startTime</label>
                <input type="text" name="startTime" id="startTime" class="form-control" placeholder="startTime">
                <td></td>


                <label for="endTime" class="sr-only">endTime</label>
                <input type="text" name="endTime" id="endTime" class="form-control" placeholder="endTime">
                <td></td>

                <label for="environment" class="sr-only">environment</label>
                <input type="text" name="environment" id="environment" class="form-control"
                       placeholder="daily or online">
                <td></td>

                <div class="checkbox">
                </div>
                <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
            </form>

    </body>
</html>

在页面源代码中找不到<form:error>标签,因此当输入验证失败时,错误信息将不会显示。这是为什么? device.css是从bootstrap sample复制的。

1 个答案:

答案 0 :(得分:0)

首先在服务器上处理JSP表单,然后将HTML发送到浏览器。这就是为什么你没有在浏览器中看到<form:errors/>,因为它是JSP的代码,而不是HTML。

您的验证错误是否仍然显示在浏览器窗口中?

如果您使用的是Chrome浏览器,则可能无法在HTML源代码中看到验证错误的标记,因为它会在您打开源视图窗口时发出新的请求。