带有Angular.js验证的jsp表单和servlet

问题描述 投票:0回答:1

我尝试做一个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

java javascript angularjs jsp servlets
1个回答
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或是否与您进行的其他操作有关,即不是帖子的一部分。

编辑:看了一些之后,我认为您应该对代码进行一点重做。似乎最好不要使用“提交”按钮类型,而只是将“提交”行为发送给您在其中验证表单的函数。所以看起来像这样:

$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提交表单,它不会刷新页面。

© www.soinside.com 2019 - 2024. All rights reserved.