ajax 异步提交 struts2 表单

Struts2 对Ajax提供了很好的支持,使用的是DOJO,如果你不会使用DOJO DWR的话,其实使用Struts2 提供的Ajax JSP tags 也是可以的,使用很简单。
使用名为Stream的Result 即可。
配置文件

Java代码  
  1. <action name="ajaxAction" class="com.yaxing.action.UnitInfoAction"  
  2.             method="findByUU">   
  3.             <result type="stream">   
  4.                 <param name="contentType">text/html</param>   
  5.                 <param name="inputName">inputStream</param>   
  6.   
  7.             </result>   
  8.   
  9.         </action>  
<action name="ajaxAction" class="com.yaxing.action.UnitInfoAction"
			method="findByUU">
			<result type="stream">
				<param name="contentType">text/html</param>
				<param name="inputName">inputStream</param>

			</result>

		</action>


Action:
Java代码  
  1. public String findByUU() throws Exception {   
  2.         try {   
  3.             Map session = ActionContext.getContext().getSession();   
  4.   
  5.             String ids = session.get("userId").toString();   
  6.                
  7.             listUnitInfo = this.unitInfoService.findByUU(java.net.URLDecoder.decode(userName, "UTF-8"), ids);   
  8.                
  9.             if (listUnitInfo.size() > 0) {   
  10.                 inputStream = new ByteArrayInputStream(   
  11.                         "true".getBytes("utf-8"));   
  12.             } else {   
  13.                 inputStream = new ByteArrayInputStream(   
  14.                         "此单位帐号可以使用!".getBytes("utf-8"));   
  15.             }   
  16.   
  17.         } catch (Exception e) {   
  18.             e.printStackTrace();   
  19.             return INPUT;   
  20.         }   
  21.         return SUCCESS;   
  22.     }  
public String findByUU() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();

			String ids = session.get("userId").toString();
			
			listUnitInfo = this.unitInfoService.findByUU(java.net.URLDecoder.decode(userName, "UTF-8"), ids);
			
			if (listUnitInfo.size() > 0) {
				inputStream = new ByteArrayInputStream(
						"true".getBytes("utf-8"));
			} else {
				inputStream = new ByteArrayInputStream(
						"此单位帐号可以使用!".getBytes("utf-8"));
			}

		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
		return SUCCESS;
	}


页面:
Java代码  
  1. <tr>   
  2.                                     <td height="15" bgcolor="#FFFFFF">   
  3.                                         <div align="center" class="STYLE1">   
  4.                                             <div align="center">单位名称</div>   
  5.                                         </div>   
  6.                                     </td>   
  7.                                     <td height="15" bgcolor="#FFFFFF">   
  8.                                         <div align="center">   
  9.                                             <span class="STYLE1"> <input type="text" id="name"  
  10.                                                 name="unitInfo.name" style="width: 120px" /> </span>   
  11.                                         </div></td>   
  12.                                     <td height="15" bgcolor="#FFFFFF">   
  13.                                         <div align="center">   
  14.                                             <span class="STYLE1">   
  15.                                                 <div id="nameTip" style="width: 120px"></div> </span>   
  16.                                         </div></td>   
  17.   
  18.                                 </tr> 

JS:
Java代码 复制代码  收藏代码
  1. <script language="javascript">   
  2. var myRequest;   
  3. var flag;   
  4. function t1() {   
  5.     //准备向后台传输的数据   
  6.     var userName = document.getElementById("userName1").value;   
  7.     alert(userName);   
  8.     url = encodeURI(userName);   
  9.     url = encodeURI(url);   
  10.     //Ajax   
  11.     myRequest = new ActiveXObject("Msxml2.XMLHTTP");   
  12.     myRequest.onreadystatechange = t2;   
  13.     myRequest.open("GET""unitInfo/ajaxAction.action?userName=" + url, true);   
  14.     myRequest.send(null);   
  15. }   
  16. function t2() {   
  17.     if (myRequest.readyState == 4) {   
  18.         var ret = myRequest.responseText;   
  19.         //后续处理   
  20.         if (ret == "true") {   
  21.             flag = false;   
  22.             document.getElementById("userName1Tip").innerHTML = "单位帐号重复,无法使用";   
  23.         } else {   
  24.             flag = true;   
  25.             document.getElementById("userName1Tip").innerHTML = "此单位帐号可以使用";   
  26.         }   
  27.     }   
  28. }   
  29.   
  30. function checkForm() {   
  31.     return flag;   
  32. }   
  33. </script>  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值