ajax跨页面提交表单

前面提到过重复提交表单问题,处理token口令校验、重定向之外,还有一种经常使用到的方法就是新页面处理表单提交,完成后关闭当前页面,并刷新之前发送请求的页面。

这里使用了artDialog.js

1、文件结构


2、user.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>user列表</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0"> 
	<script type="text/javascript" src="/MySSH2/jquery-1.7.js"></script>
	<script type="text/javascript" src="/MySSH2/artDialog.js?skin=default"></script>
	<script type="text/javascript">
  	function openA(){
  	window.open("/MySSH2/user/manage_addUI");
  	}
  </script>
  </head>
  <body>
   <br/>
    <a href="<s:url action="manage_addUI" namespace="/user"/>">添加用户</a>
    <a href="javascript:void(0)" οnclick="openA()">添加用户</a>
     <br/>
     用户列表:<br/>
   <s:iterator value="#request.users">
   id:<s:property value="id"/><br/>
   name:<s:property value="name"/><br/>
   </s:iterator>
   
  
  </body>
</html>
3、userAdd.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>用户添加</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0"> 
	<script type="text/javascript" src="/MySSH2/jquery-1.7.js"></script>
  <script type="text/javascript">
  function AddUser(){
  var submitData = $('#userForm').serialize();  
  console.log(submitData);
  	$.ajax({
				type : "post",
				url : "/MySSH2/user/manage_add",
				cache : false,
				data: submitData, 
				dataType : 'json',
				success : function(result) {
				<span style="white-space:pre">		</span>if(result.success){
						 window.opener.art.dialog({time:2,content:'保存成功'});
						 setTimeout(function(){window.opener.location.reload();},3);
						}
						else{
						<span style="white-space:pre">	</span> window.opener.art.dialog({time:2,content:'保存失败'});
							 setTimeout(function(){window.opener.location.reload();},3);
							}
							window.close();
						},
				error : function(XMLHttpRequest, textStatus,
						errorThrown) {
								alert("error");
						}
				});
  }
  </script>
  </head>
  
  <body>
  	<s:form id="userForm" action="manage_add" namespace="/user" method="post">
  		用户名:<s:textfield name="user.name"/><br/><s:token></s:token>
  		<input type="button" value="保存" οnclick="AddUser()"/>
  	</s:form>
  </body>
</html>
4、UserManageAction.java

package com.myssh2.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.ServletException;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.myssh2.bean.User;
import com.myssh2.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@Controller @Scope("prototype")
public class UserManageAction extends ActionSupport{
	@Resource UserService userService;
	private User user;	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String addUI(){
		return "add";
	}
	
	public void add() throws ServletException, IOException{
	
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");   
        PrintWriter out = ServletActionContext.getResponse().getWriter();
        try {
        	userService.addUser(user);
    		ActionContext.getContext().put("message", "保存成功");
    		out.write("{\"success\":true}");
		} catch (Exception e) {
			 e.printStackTrace();
			 out.write("{\"success\":false,\"msg\":\"error\"}");
		}
	}
}
页面效果

提交表单时使用$('#userForm').serialize();序列化表单数据

 window.opener.art.dialog({time:2,content:'保存成功'});则是返回使用window.open的页面(或者理解为父页面),并调用artDialog插件的定时关闭dialog

setTimeout(function(){window.opener.location.reload();},3);使用定时器刷新使用window.open的页面(或者理解为父页面),dialog和reload的时间设置问题需重新调整。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Ajax无刷新提交表单是通过使用JavaScript的Ajax技术,将表单数据异步发送给服务器进行处理,而无需刷新整个页面。 首先,我们需要创建一个表单,并为表单添加一个提交事件的监听器。在提交事件发生时,阻止表单的默认提交行为。 然后,使用JavaScript中的FormData对象获取表单中的数据。我们可以通过FormData的append方法将表单元素的值添加到FormData对象中。 接下来,创建一个XMLHttpRequest对象,并使用该对象向服务器发送请求。我们可以使用open方法来指定请求的类型、URL和是否异步。然后,为XMLHttpRequest对象添加一个onreadystatechange事件的监听器,以便在服务器响应返回时进行处理。 在服务器响应返回后,我们可以通过XMLHttpRequest对象的readyState和status属性来判断服务器的响应状态。当readyState为4且status为200时,表示服务器响应成功。 如果服务器返回的数据需要展示在页面上,我们可以使用responseText或responseXML属性来获取服务器返回的内容。 最后,我们可以根据服务器返回的结果,动态更新页面的内容,而无需刷新整个页面。可以使用JavaScript来修改DOM元素的内容或样式。 总结起来,Ajax无刷新提交表单使得我们能够将表单数据异步发送给服务器,而无需刷新整个页面。这种技术可以提高用户体验,并有效减少页面刷新的次数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz_cl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值