使用Angular.js验证的jsp表单和servlet

时间:2014-05-08 08:15:57

标签: java javascript angularjs jsp servlets

我尝试使用jsp表单,该表单会在servlet验证后吸引Angular.js

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

AfterFormServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AfterFormServlet
 */
@WebServlet("/AfterFormServlet")
public class AfterFormServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AfterFormServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Hi");
    }

}

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_建议 -

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

进行验证,但没有进入servlet

和 -

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

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

1 个答案:

答案 0 :(得分:2)

您的表单没有action属性,这意味着在使用ng-submit指令时,它会阻止导致表单无法提交的默认行为。有关此信息https://docs.angularjs.org/api/ng/directive/ngSubmit

修正:

action=""属性添加到表单标记。

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

我还注意到你有一些名为active的属性,我不确定这只是一个未命中类型,并且假设是action或者它是否与你得到的其他东西有关goin就是帖子的一部分。

修改 看了一下后,我认为你应该稍微修改你的代码。不使用提交按钮类型似乎是一个更好的流程,而只是将提交行为发送到您验证表单的函数。所以它看起来像这样

$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');
    }

};

和你的HTML

    <!-- CHANGE HERE -->
    <form action="AfterFormServlet" method="POST" name="userForm" 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>    
        <!-- CHANGE HERE -->
        <button ng-click="submitForm(userForm.$valid)" class="btn btn-primary">Submit</button>    
    </form> 

另外,您可以使用角度中的$http提供程序通过AJAX提交表单,它不会刷新页面