Java jsp表单和servlet以及Angular.js验证

Java jsp表单和servlet以及Angular.js验证,java,javascript,angularjs,jsp,servlets,Java,Javascript,Angularjs,Jsp,Servlets,我尝试做一个jsp表单,在Angular.js验证之后,该表单将吸引servlet 我有3个主要文件- default.jsp <!DOCTYPE html> <html> <head> <!-- CSS --> <link rel="stylesheet" href="css/bootstrap.min.css" /> <!-- JS --> <script src="js/angular.js"></

我尝试做一个
jsp
表单,在
Angular.js
验证之后,该表单将吸引
servlet

我有3个主要文件-

default.jsp

<!DOCTYPE html>
<html>
<head>
<!-- CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css" />

<!-- JS -->
<script src="js/angular.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="validationApp" ng-controller="mainController">
    <div class="container">

        <!-- =================================================================== -->
        <!-- FORM ============================================================== -->
        <!-- =================================================================== -->

        <!-- pass in the variable if our form is valid or invalid -->
        <form action="AfterFormServlet" method="POST" name="userForm"
            ng-submit="submitForm(userForm.$valid)" novalidate>

            <!-- FIRST NAME -->
            <div class="form-group"
                ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$pristine }">
                <label>First name</label> <input type="text" name="name"
                    class="form-control" ng-model="user.name"
                    ng-pattern="/^[a-z ,.'-]+$/i" required>
                <p ng-show="userForm.name.$invalid && !userForm.name.$pristine"
                    class="help-block">Enter a valid last first name.</p>
            </div>    
            <button type="submit" class="btn btn-primary">Submit</button>    
        </form>    
    </div>
</body>
</html>
app.js

// create angular app
var validationApp = angular.module('validationApp', []);

// create angular controller
validationApp.controller('mainController', function($scope) {

    // function to submit the form after all validation has occurred            
    $scope.submitForm = function(isValid) {

        // check to make sure the form is completely valid
        if (isValid) {
            alert('Thanks, your order was Sent');
        }
        else {
            alert('Invalid form');
        }

    };

});
运行它之后,我进入表单,填写所需字段,单击
submit
,它进入servlet,即使验证失败

如何让它工作

更新:

根据@ug_uu建议-

<form action="" active="AfterFormServlet" method="POST" name="userForm"
             ng-submit="submitForm(userForm.$valid);" novalidate>

使验证生效,但即使验证失败,它也会进入
servlet

您的表单没有
action
属性,这意味着当使用
ng submit
指令时,它将防止导致表单不提交的默认行为。关于它的信息在这里

修复:

action=”“
属性添加到表单标签

<form active="AfterFormServlet" action="" method="POST" name="userForm"
        ng-submit="submitForm(userForm.$valid)" novalidate>
以及您的html:


名字

输入有效的姓氏和姓氏

提交

或者,您可以使用angular中的
$http
提供程序通过AJAX提交表单,但它不会刷新页面。

我的建议:将表单操作更改为
action=“”
,并使用
ng submit返回true
。还有一个快速的问题,警报显示了吗?@ug\ux:你能解释一下怎么写吗?是的,
警报将显示。编辑后,仍然不会执行验证。
<form action="AfterFormServlet" method="POST" name="userForm"
             ng-submit="submitForm(userForm.$valid);" novalidate> 
<form active="AfterFormServlet" action="" method="POST" name="userForm"
        ng-submit="submitForm(userForm.$valid)" novalidate>
$scope.submitForm = function(isValid) {

    // check to make sure the form is completely valid
    if (isValid) {
        alert('Thanks, your order was Sent');
        $scope.userForm.submit();
    }
    else {
        alert('Invalid form');
    }
};