Java Web视频讲义例子PPT表单自动提交和ajax方式提交,serializeArray()【诗书画唱】


概述

一般就是用submit。
自动提交方式START


自动提交方式END
js手动提交START


js手动提交END
Ajax提交(不会页面跳转版,会把界面的代码全放在data中)START

如果想界面跳转就:

当然也可以用id,val等:


Ajax提交(不会页面跳转版)END
PPT START














使用表单序列化方法serialize(),会智能的获取指定表单内的所有元素。这样,在面对大量表单元素时,会把表单元素内容序列化为字符串,然后再使用 Ajax 请求
除了serialize()方法,还有一个可以返回 JSON 数据的方法:.serializeArray()。这个方法可以直接把数据整合成键值对的 JSON 对象。
PPT END
讲义 START

表单提交:
1、有页面刷新的(自动)提交(有页面跳转)和ajax提交(没有页面刷新)
2、表单传参方式
3、表单验证
type="submit"方式提交,自动提交有页面刷新
type="button"绑定一个点击事件提交,手动提交,可能有页面刷新,也可能没有页面刷新
ajax提交方式没有页面刷新
讲义 END
例子 START

package com.jy.servlet;
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 LoginServlet
*/
@WebServlet("/ls")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String act = request.getParameter("act");
String pwd = request.getParameter("pwd");
System.out.println("账号是:" + act + ",密码是:" + pwd);
String flag = request.getParameter("flag");
System.out.println(flag);
request.getRequestDispatcher("manage.jsp")
.forward(request, response);
//查询出了商品的信息
//苹果,6.5,红富士,橙子,5.5,脐橙
//response.getWriter().write("Hello world");
}
}
submit自动提交START

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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>
<head>
<base hreff="<%=basePath%>">
<title></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">
<style type="text/css">
* {
font-size: 30px;
}
</style>
<script type="text/javascript">
function doCheck(){
var flag = true;
//验证时修改flag的值来确定是否执行提交的servlet
var act = document.getElementById('act').value;
var len = act.length;
if(len > 30 || len < 6) {
flag = false;
alert('账号的长度必须在6到30位之间');
}
return flag;
}
</script>
</head>
<body>
<form action="ls?flag=false" method="post" onsubmit="return doCheck();">
<label>账号:</label>
<input type="text" name="act" id="act"/>
<br>
<label>密码:</label>
<input type="password" name="pwd" id="pwd"/>
<br>
<input type="submit" value="提交" />
</form>
</body>
</html>

submit自动提交END
手动表单提交START

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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>
<head>
<base hreff="<%=basePath%>">
<title></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">
<style type="text/css">
* {
font-size: 30px;
}
</style>
<script type="text/javascript">
function doSb(){
//alert('手动表单提交');
//设置表单的提交路径
//document.getElementById('myForm').action = 'ls';
//提交表单
if(doCheck()) {
document.getElementById('myForm').submit();
}
}
function doCheck(){
var flag = true;
//验证时修改flag的值来确定是否执行提交的servlet
var act = document.getElementById('act').value;
var len = act.length;
if(len > 30 || len < 6) {
flag = false;
alert('账号的长度必须在6到30位之间');
}
return flag;
}
</script>
</head>
<body>
<form id="myForm" action="ls?flag=true" method="post">
<label>账号:</label>
<input type="text" name="act" id="act" />
<br>
<label>密码:</label>
<input type="password" name="pwd" id="pwd"/>
<br>
<input type="button" value="提交" onclick="doSb();" />
</form>
</body>
</html>

手动表单提交END
Ajax提交START

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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>
<head>
<base hreff="<%=basePath%>">
<title></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">
<style type="text/css">
* {
font-size: 30px;
}
</style>
<script type="text/javascript" srcc="js/jquery-1.11.0.js"></script>
<script type="text/javascript">
function doAjax(){
//获取输入的账号和密码
//var a = $('#act').val();
//var p = $('#pwd').val();
if(doCheck()){
//方法1:
//var ps = $('#frm').serialize();
//方法2:
//获取表单中的所有属性,并且封装在obj对象中
//[{"name":"act","value":"admin"},{"name":"pwd","value":"123"}]
var arr = $('#frm').serializeArray();
var params = {};
for(var i = 0;i < arr.length;i ++) {
var attr = arr[i];
params[attr.name] = attr.value;
}
//提交一个ajax请求
$.ajax({
//方法1
//url: 'ls?flag=true&' + ps,
//data: {
// act: a,
// pwd: p
//},
//方法2
data: params,
type: 'POST',
success: function(data){
alert(data);
}
});
}
}
function doCheck(){
var flag = true;
//验证时修改flag的值来确定是否执行提交的servlet
var act = $('#act').val();
var len = act.length;
if(len > 30 || len < 6) {
flag = false;
alert('账号的长度必须在6到30位之间');
}
return flag;
}
</script>
</head>
<body>
<form id="frm" action="" method="post">
<label>账号:</label>
<input type="text" name="act" id="act" />
<br>
<label>密码:</label>
<input type="password" name="pwd" id="pwd"/>
<br>
<input type="button" value="提交" onclick="doAjax();" />
</form>
</body>
</html>

Ajax提交END

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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>
<head>
<base hreff="<%=basePath%>">
<title></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">
</head>
<body>
<h1>管理页面</h1>
</body>
</html>