---
title: "异步提交表单多个应用"
author: "jasstion"
date: "2009-09-13T10:35:33Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-09-13T12:34:31Z"
id: 467694
url: "https://jasstion.iteye.com/blog/467694"
---

今天，在完成文章留言功能的时候，想实现用户留言信息异步提交，并在最开始的页面显示留言是否成功与否！经过一个上午的思考以及实践，现有两种方法可以实现其功能。  
  第一种：利用JQuery的Ajax函数：  
function load(){  
 var str=$("#replyform").serialize();  
$.ajax({  
    type: "POST",  
    cache: false,  
    [url:"/On/myblog/Addleftwd.action](/admin/blogs/467694/%22/On/myblog/Addleftwd.action)",  
    data:str,  
    success: function(msg){  
 $("#result").html(msg);  
    }  
  });  
}  
第二种：利用JQuery的load()函数：  
function load(){  
 var str=$("#replyform").serialize();  
  alert(str);  
  $("#result").load("/On/myblog/Addleftwd.action?"+str);  
}  
第三种：直接利用Struts 2.1.6中提供的异步提交标签：  
又有两种：  
&lt;s:url var="l" action="Addleftwd.action" namespace="/myblog"&gt;&lt;/s:url&gt;  
&lt;sx:a  href="%{l}"  formId="replyform" targets="result"&gt;  
     点击此链接  
&lt;/sx:a&gt;  
此标签放在提交表单外；  
&lt;sx:submit id="reply\_go" value="提交"  targets="result"&gt;&lt;/sx:submit&gt;  
此标签放在提交表单内部。

在利用Struts中自带的这些异步提交标签时，有事会发现一些属性不会被对应的表单域值所填从，不知道这是不是一个小BUG。  
但是在利用前两种方法时，需要将表单域序列化，然后做最参数传递给Action，结果在留言内容部分出现乱码，试着用下面的方式在Action内部解决，还是显示乱码：  
public Leftwd leftwd;  
  public String addleftwd()  
  { Session sess=new BaseHibernateDAO().getSession();  
  Transaction tx=null;  
  try{tx=sess.beginTransaction();  
  byte\[] temp =leftwd.getContent().getBytes("UTF-8");  
  leftwd.setContent(new String(temp, "GBK"));  
  //leftwd.getContent()=new String(getBytes("UTF-8"),"gb2312");  
  Timestamp datetime = new Timestamp(new Date().getTime());  
  leftwd.setDate(datetime);  
  sess.saveOrUpdate(leftwd);  
  tx.commit();  
  setMessage("发表留言成功！");  
  }  
  catch(Exception e ){if(tx!=null){tx.rollback();setMessage("抱歉！服务器繁忙！请稍后再试！");}  
  }  
  finally{sess.close(); return "addleftwd";}

   
  }  
希望高人指点一下！谢谢！！  
我的文章显示界面：  
&lt;%@ page contentType="text/html; charset=GBK" %&gt;  
&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;  
&lt;%@ taglib prefix="sx" uri="/struts-dojo-tags" %&gt;  
&lt;%@ page contentType="text/html; charset=GBK" %&gt;  
 &lt;%

 //得到本页的参数内容  
 int totals=0,allrecords=1,prePageNo=1,pageNo=0,nextPageNo=1,preOk=1,nextOk=1;  
   
      if(request.getParameter("allrecprds")!=null){allrecords = Integer.parseInt(request.getParameter("allrecords").toString());}  
   if(request.getParameter("pageno")!=null){pageNo = Integer.parseInt(request.getParameter("pageno").toString());  
   
   
   }  prePageNo = pageNo;  
 nextPageNo=pageNo;  
 if (pageNo==0) nextOk=0;  
 if (pageNo\*3&gt;allrecords) preOk=0;  
   
 //计算上一页和下一页的页码及按钮的状态

%&gt;  
&lt;html&gt;  
&lt;head&gt;  
&lt;sx:head debug="true" extraLocales="en-us,nl-nl,de-de"/&gt;  
&lt;script type="text/javascript" src="/On/JS/jquery-1\[1].3.2.js"&gt;&lt;/script&gt;  
&lt;script type="text/javascript" src="/On/JS/nav.js"&gt;&lt;/script&gt;  
&lt;s:include value="/all.jsp"&gt;&lt;/s:include&gt;  
&lt;script type="text/javascript" src="/On/tinymce/jscripts/tiny\_mce/tiny\_mce.js"&gt;&lt;/script&gt;  
&lt;script type="text/javascript"&gt;  
 tinyMCE.init({  
  mode : "textareas",  
  theme : "simple"  
     
 });

function load(){  
 var str=$("#replyform").serialize();  
$.ajax({  
    type: "POST",  
    cache: false,  
    [url:"/On/myblog/Addleftwd.action](/admin/blogs/467694/%22/On/myblog/Addleftwd.action)",  
    data:str,  
    success: function(msg){  
 $("#result").html(msg);  
    }  
  });  
}  
   
&lt;/script&gt;

&lt;title&gt;无标题文档&lt;/title&gt;  
&lt;style&gt;  
body{  
font-family:Arial, Helvetica, sans-serif;  
font-size:14px;

}  
#topicdetail\_content{  
border:1px solid #996633;  
margin-left:10%;  
width:90%;  
}  
#content{  
float:right;  
width:80%;  
border:1px solid #996633;  
height:800px;  
margin-top:0px;  
}  
#header{  
float:left;  
line-height:14px;  
font-family:Arial, Helvetica, sans-serif;  
font-size:20px;  
color:#FFCC33;  
margin-left:0px;  
width:100%;  
height:30px;  
background:url(/On/jpg/nav.gif) repeat;  
}  
#header #headerul{  
margin-top:1px;  
margin-bottom:1px;  
margin-left:0px;  
list-style:none;  
}  
#header #headerul li{  
text-align:center;  
padding:5px 5px 5px 5px;  
border: 1px solid #ffffff;

display: block;  
float:left;  
}  
#header #author{  
width:20%;

}  
#picture{  
width:100%;  
height:300px;  
}  
#header #topic{  
width:80%;

}  
#authorinfo{  
margin-left:0px;  
list-style:none;  
}  
#authorinfo #authorinfoul{  
margin-left:0px;}  
#authorinfoul li{  
margin-left:0px;  
}  
#leftbar{  
border:1px solid #996633;  
margin-left:0px;  
width:20%;  
float:left;  
}  
#elm{  
font-family:Arial, Helvetica, sans-serif;

}  
#leftwd\_content{  
float:right;  
width:80%;  
border:1px solid #996633;  
margin-top:0px;  
}

 

#leftwd\_leftbar{  
border:1px solid #996633;  
margin-left:0px;  
width:15%;  
float:left;  
height:250px;  
}  
#authorinfo{  
margin-left:0px;  
list-style:none;  
}  
#authorinfo #authorinfoul{  
margin-left:0px;}  
#authorinfoul li{  
margin-left:0px;  
}

#rightbar{  
float:left;  
height:250px;  
width:80%;  
}  
#firstbar{  
border:1px solid #FFFFFF;  
width:100%;  
height:10%;}  
#leftword{  
width:100%;  
height:100%;  
}  
#lastbar{  
border:1px solid #FFFFFF;

width:100%;  
height:10%;  
margin-bottom:0px;}  
#changepage{  
float:right;  
width:80%;  
border:1px solid #FFCCFF;  
margin-top:0px;  
}  
#reply{  
float:right;  
width:80%;  
border:1px solid #996633;  
margin-top:0px;  
}  
#reply\_go{  
width:50px;  
height:20px;  
display:block;  
border:1px solid #FFCCFF;  
}  
#changepage a{  
background:#3366CC;  
float:right;  
width:60px;  
display:block;  
color:#FFFFFF;

 

}  
&lt;/style&gt;  
&lt;/head&gt;

&lt;body&gt;  
&lt;div id="topicdetail\_content"&gt;  
 &lt;div id="header"&gt;  
  &lt;ul id="headerul"&gt;  
  &lt;li id="author"&gt;作者&lt;/li&gt;  
  &lt;li id="topic"&gt;正文&lt;/li&gt;  
  &lt;/ul&gt;  
  &lt;/div&gt;&lt;!--end header--&gt;  
&lt;div id="leftbar"&gt;  
&lt;div id="authorinfo"&gt;  
  &lt;ul id="authorinfoul"&gt;  
  &lt;li id="photo"&gt;&lt;a href="/On/myblog/showblog.action?mem=${member.id}"&gt;&lt;img src="${member.picture}" id="picture"/&gt;&lt;/a&gt;&lt;/li&gt;  
   &lt;li&gt;姓名:&lt;s:property value="member.name"/&gt;&lt;/li&gt;  
  &lt;li&gt;浏览次数：&lt;s:property value="member.blogtimes"/&gt; &lt;/li&gt;  
  &lt;li&gt;积分:&lt;s:property value="member.money"/&gt;&lt;/li&gt;  
 &lt;li&gt;来自于:&lt;s:property value="member.adress"/&gt;&lt;/li&gt;  
 &lt;li&gt;是否在线&lt;/li&gt;  
 &lt;li&gt;我要给他发送信息&lt;/li&gt;  
  &lt;/ul&gt;  
&lt;/div&gt;  
&lt;/div&gt;&lt;!--end leftbar--&gt;  
&lt;div id="content"&gt;  
&lt;H1 align="center"&gt;  
&lt;s:property value="topicblog.name"/&gt;&lt;/H1&gt;  
 &lt;h4 align="center"&gt;&lt;s:property value="topicblog.data"/&gt;&lt;s:property value="topicblog.times"/&gt;&lt;br&gt;&lt;/h4&gt;

 &lt;s:textarea readonly="true"  id="elm" name="elm1" value="%{topicblog.content}" rows="42" cols="70" style="width:100%"/&gt;  
 &lt;/div&gt;  
&lt;!--留言显示--&gt;  
&lt;s:iterator value="leftwdinfo"&gt;  
&lt;div id="leftwd\_content"&gt;

&lt;div id="leftwd\_leftbar"&gt;  
&lt;div id="authorinfo"&gt;  
  &lt;ul id="authorinfoul"&gt;  
    &lt;li&gt;姓名:&lt;s:property value="name"/&gt;&lt;/li&gt;  
  &lt;li id="photo"&gt;&lt;img src="${picture}" width="160" height="170" /&gt;&lt;/img&gt;&lt;/li&gt;  
 &lt;li&gt;博客文章:&lt;s:property value="topicnum"/&gt;&lt;/li&gt;  
  &lt;li&gt;积分:&lt;s:property value="money"/&gt;&lt;/li&gt;  
 &lt;li&gt;来自于:&lt;s:property value="adress"/&gt;&lt;/li&gt;  
 &lt;li&gt;是否在线&lt;/li&gt;  
 &lt;li&gt;我要给他发送信息&lt;/li&gt;  
  &lt;/ul&gt;  
&lt;/div&gt;  
&lt;/div&gt;&lt;!--end leftbar--&gt;  
&lt;div id="rightbar"&gt;  
&lt;div id="firstbar"&gt;  
&lt;strong&gt;发表时间:&lt;s:property value="date"/&gt;&lt;/strong&gt;  
&lt;/div&gt;  
&lt;div id="leftword"&gt;&lt;s:property value="content"/&gt;&lt;/div&gt;  
&lt;div id="lastbar"&gt;&lt;/div&gt;  
&lt;/div&gt;  
&lt;/div&gt;&lt;!--end leftwd\_content--&gt;  
&lt;/s:iterator&gt;   
&lt;div id="changepage"&gt;  
总共&lt;s:property value="%{attr.allrecords}"/&gt;留言  
&lt;a href="showtopicdetail.action?id=${topicblog.id}&mem=${topicblog.memberId}&pageno=${request.pageno}&up=false "&gt;上一页&lt;/a&gt;  
&lt;a href="showtopicdetail.action?id=${topicblog.id}&mem=${topicblog.memberId}&pageno=${request.pageno}&up=true "&gt;下一页&lt;/a&gt;  
&lt;/div&gt;  
&lt;!-- 留言板块 --&gt;  
&lt;div id="reply"&gt;  
&lt;strong&gt;发表评论&lt;/strong&gt;&lt;hr /&gt;  
&lt;div id="result"&gt;  
&lt;/div&gt;  
&lt;div id="reply\_content"&gt;  
&lt;s:if test="%{#session.member!=null}"&gt;  
&lt;s:form id="replyform" method="post" theme="xhtml"&gt;  
&lt;s:hidden value="%{topicblog.id}" name="leftwd.topicBlogId"&gt;&lt;/s:hidden&gt;  
&lt;s:text name="%{topicblog.id}"&gt;&lt;/s:text&gt;  
&lt;s:hidden value="%{session.member.id}" name="leftwd.id"&gt;&lt;/s:hidden&gt;  
&lt;s:textarea  id="elm"  name="leftwd.content" rows="6" cols="8" style="width:100%"/&gt;  
&lt;a href="javascript:load()"&gt;&lt;img src="/On/jpg/download.gif"/&gt;&lt;/a&gt;  
&lt;sx:submit id="reply\_go" value="提交"  targets="result"&gt;&lt;/sx:submit&gt;  
&lt;/s:form&gt;  
 &lt;s:url var="l" action="Addleftwd.action" namespace="/myblog"&gt;&lt;/s:url&gt;  
&lt;sx:a  href="%{l}"  formId="replyform" targets="result"&gt;  
     点击此链接  
&lt;/sx:a&gt;

&lt;/s:if&gt;  
&lt;s:else&gt;  
请先&lt;a href="/On/login.jsp"&gt;登录&lt;/a&gt;或&lt;a href="/On/register.jsp"&gt;注册&lt;/a&gt;后才能留言！  
&lt;/s:else&gt;

&lt;/div&gt;  
&lt;/div&gt;  
&lt;/div&gt;  
&lt;/body&gt;  
&lt;/html&gt;