代码下载: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);
    }
}

最新文章

  1. 如何在高并发环境下设计出无锁的数据库操作(Java版本)
  2. Prim算法(三)之 Java详解
  3. COM调用 &ndash; VB、PB
  4. sendEmail的使用
  5. 【BZOJ 1901】【Zju 2112】 Dynamic Rankings 动态K值 树状数组套主席树模板题
  6. Xcode 项目文件介绍
  7. 【大数比较】NYOJ-73
  8. abap中的弹出窗体函数
  9. C语言内存对齐原理
  10. 【笔记】JS中的数组方法
  11. html基础加强2
  12. jQueryUI中Datepicker(日历)插件使用
  13. Python摸爬滚打之day04----基本数据类型(列表,元组)
  14. LeetCode题解之 Binary Tree Preorder Traversal
  15. APP快速搭建框架
  16. 卿学姐与公主 UESTC - 1324 分块模板题
  17. js匿名函数和闭包总结
  18. Maven实战(十)利用 Nexus 来构建企业级 Maven 仓库
  19. mac php项目除了首页全飘not found
  20. python编程遇见的异常

热门文章

  1. 使用spyder3调试python程序
  2. 开发者工具删除元素Delete Element
  3. Git使用规范流程(转载)
  4. 【bzoj4753】[Jsoi2016]最佳团体 分数规划+树形背包dp
  5. C++ 错误解决 —— internal compiler error
  6. spring中MessageSource的配置使用方法1
  7. webpack打包字体图标报错的解决办法
  8. hdu 2857 点在直线上的投影+直线的交点
  9. 挑战程序设计2 KD树
  10. 【POJ1276】Cash Machine(多重背包单调队列优化)