gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:51:01 24 4
gpt4 key购买 nike

我尝试创建一个 jsp 表单,在 Angular.js 验证后吸引 servlet

我有 3 个主要文件 -

默认.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');
}

};

});

运行后,我进入表单,填写必填字段,单击提交,即使验证失败,它也会进入 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

最佳答案

您的表单没有 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>

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

关于java - jsp 表单和带有 Angular.js 验证的 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23536314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com