AngularJS的表单验证提交示例

简介:

代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsFormSubmit.rar

 

前台代码:

复制代码
<%@ page contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html ng-app="notesApp">
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <meta charset="utf-8">
    <script src="angular1.4.6.min.js"></script>
  </head>
  
  <body ng-controller="MainCtrl as ctrl">
    <form action="Verify" id="form1">
          Name:<input type="text" name="name" ng-model="ctrl.username"/><br/>
          You typed:{{ctrl.username}}<br/>
          Pswd:<input type="password" name="pswd"  ng-model="ctrl.password"/><br/>
          You typed:<span ng-bind="ctrl.password"></span><br/>
          <button type="button" ng-click="ctrl.reset()">Reset</button> <!-- 注意这里不写 type="button" 会导致表单提交-->
          <button type="button" ng-click="ctrl.submit()">Submit</button>

    </form>
  </body>
</html>

<script type="text/javascript">
<!--
    angular.module('notesApp',[])
     .controller('MainCtrl',[function(){
       var self=this;

       self.reset=function(){
               self.username="";
               self.password="";
       };

       // 在Chrome正常,在FirxFox中self.username,self.password是undefined
       self.submit=function(){  
               alert("self.username="+self.username);
               alert("self.password="+self.password);
               if(self.username!="" && self.password!=""){
                   alert(2);
                   document.forms[0].submit();
               }else{
                   alert(3);
                   alert("用户名或密码不全");
               }
               alert(4);
               
       };
       
     }]);
//-->
</script>
复制代码

后台代码:

复制代码
package com.test;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class VerifyServlet extends HttpServlet {
    private static final long serialVersionUID = 56890894234786L;
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        request.setCharacterEncoding("UTF-8");
        
        String name=request.getParameter("name");
        String pswd=request.getParameter("pswd");
        
        request.setAttribute("name", name);
        System.out.println("name="+name);
        request.setAttribute("pswd", pswd);
        System.out.println("pswd="+pswd);

        RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
        dispatcher.forward(request, response);
        return;
    }
        
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {
        doPost(request, response);
    }
}
复制代码

 
















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/xiandedanteng/p/7416061.html,如需转载请自行联系原作者

相关文章
|
6月前
|
前端开发 JavaScript 开发者
详细介绍 AngularJS 表单的各种特性、用法和最佳实践
详细介绍 AngularJS 表单的各种特性、用法和最佳实践
115 1
|
JavaScript 前端开发 API
让Angular自定义组件支持form表单验证
让Angular自定义组件支持form表单验证
165 0
|
JavaScript 容器
AngularJS----服务,表单,模块
AngularJS----服务,表单,模块
134 0
AngularJS----服务,表单,模块
|
JavaScript 容器 数据挖掘
Angularjs 与三方js插件配合使用,并通过模板动态解析angularjs 语法
在一个静态见面上做数据分析,由于前后端分离 前端使用Angularjs框架,后端使用RESTFUL,如图
2706 0
|
数据格式 JSON JavaScript