基于Struts2框架下实现Ajax有两种方式,第一种是原声的方式,另外一种是struts2自带的一个插件。

js部分调用方式是一样的:

JS代码:

 function testAjax() {

     var $userNameInput = $("#ajax_username");
var userName = $userNameInput.val(); $.ajax({
url : "originAjax.action",
type : "GET",
data : "ajaxField=" + userName,
success : function(data, textStatus) {
alert(data);
}
});
}

第一种原生的实现方式:

Action中创建一个方法:

    private String username;

    public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public void checkUserName() throws IOException { HttpServletResponse response = ServletActionContext.getResponse();
PrintWriter writer = response.getWriter();
writer.print("hello " + username);
writer.flush();
writer.close();
}

struts.xml中配置:

<action name="originAjax" class="TestAction" method="checkUserName" />  

这种方式不太推荐使用。

这里重点讲解下第二种方式:

1.引入struts2-json-plugin-2.5.2.jar

2.Action中添加类似的如下代码:

private String result;
public String getResult() {
return result;
} public void setResult(String result) {
this.result = result;
}
/**
*
* AJAX for check MerchantAccount start
*
* */
private String merchantAccount; public String getMerchantAccount() {
return merchantAccount;
} public void setMerchantAccount(String merchantAccount) {
this.merchantAccount = merchantAccount;
} // AJAX for check Merchant
public String checkMerchantAccountMethod() throws IOException { AppResultJsonBean ajaxResultJsonBean = new AppResultJsonBean(); if (StarCloudStringUtils.isEmpty(merchantAccount)) { ajaxResultJsonBean.setIsOK(false);
ajaxResultJsonBean.setData(null);
ajaxResultJsonBean.setResultCode(-1);
ajaxResultJsonBean.setResultMessage("商家账号不能为空");
ajaxResultJsonBean.setOther(null); JSONObject ajaxResultJsonData = JSONObject
.fromObject(ajaxResultJsonBean); this.result = ajaxResultJsonData.toString(); return SUCCESS;
} if (!StarCloudStringUtils.isMobile(merchantAccount)) { ajaxResultJsonBean.setIsOK(false);
ajaxResultJsonBean.setData(null);
ajaxResultJsonBean.setResultCode(-2);
ajaxResultJsonBean.setResultMessage("商家账号格式不合法");
ajaxResultJsonBean.setOther(null); JSONObject ajaxResultJsonData = JSONObject.fromObject(ajaxResultJsonBean); this.result = ajaxResultJsonData.toString(); return SUCCESS;
} 。。。
MerchantBean checkMerchantBean = merchantIService.findMerchantByAccount(merchantAccount); if (checkMerchantBean != null) { ajaxResultJsonBean.setIsOK(true);
ajaxResultJsonBean.setData(null);
ajaxResultJsonBean.setResultCode(0);
ajaxResultJsonBean.setResultMessage("商家账号可用");
ajaxResultJsonBean.setOther(null); JSONObject ajaxResultJsonData = JSONObject.fromObject(ajaxResultJsonBean); this.result = ajaxResultJsonData.toString(); return SUCCESS;
} else {
ajaxResultJsonBean.setIsOK(false);
ajaxResultJsonBean.setData(null);
ajaxResultJsonBean.setResultCode(-3);
ajaxResultJsonBean.setResultMessage("商家账号不存在");
ajaxResultJsonBean.setOther(null); JSONObject ajaxResultJsonData = JSONObject.fromObject(ajaxResultJsonBean); this.result = ajaxResultJsonData.toString();
return SUCCESS;
}
}
/**
*
* AJAX for check MerchantAccount start end
*
* */

Struts.xml中配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts> <!-- AJAX
1.引入Jar包
2.所在包必须要继承自JSON-default
3.resultType是JSON
4.附加了一个参数excludeNullProperties,目的是不序列化Action里为null的字段。
5.<result>元素没有name属性,也没有跳转值
-->
<package name="struts_web_product_ajax" extends="json-default">
<!-- 新增商品信息检查账号 -->
<action name="checkMerchantAccountAction" class="controllers.actions.web.product.PrepareAddProductAction" method="checkMerchantAccountMethod">
<result type="json">
<param name="excludeNullProperties">true</param>
<param name="root">result</param>
</result>
</action>
</package>
</struts>

JS中接受返回结果:

返回JSON格式:

JS解析如下:

function checkMerchantAccountAjax() {

    var $merchantAccount = $("#merchantAccount");
var merchantAccount = $merchantAccount.val(); $.ajax({
url : "checkMerchantAccountAction",
type : "GET",
data : "merchantAccount=" + merchantAccount,
success : function(data, textStatus) {
var resultJSONData = JSON.parse(data);//注意这里必须有,因为之前返回的是result="json字符串",但并类型不是JSON
if(resultJSONData.isOK){
$merchantAccount.css("color", "black");
return true;
}else{
$merchantAccount.css("color", "red");
layer.tips(resultJSONData.resultMessage,$merchantAccount, {
tips : [3, '#3595CC'],
time : 4000
});//end tips
return false;
}//end else }//end success
});//end ajax
}// end js

Struts2实现ajax的两种方式的更多相关文章

  1. jquery的ajax和原始的ajax这两种方式的使用方法

    jquery的ajax是对原始的ajax进行的封装,方便用户的使用.下面用代码分别举例各自的使用方式. jquery的ajax发送和接收xml数据格式. $.ajax({ type: "PU ...

  2. Struts2网页面传值两种方式

    第一种方式: /** 列表 */ public String list() throws Exception { List<Role> roleList = roleService.fin ...

  3. ajax的两种方式

    get:var ajax=new XMLHttpRequest();ajax.open('get','__URL__/check_all?val='+check);ajax.send();ajax.o ...

  4. 【TP3.2 + 其他任何PHP框架】编辑、删除、添加数据,返回原分页 (ajax+form两种方式提交均可以)

    1.目的1:在如下的一个页面中,p=2,比如我们删除数据id=13,通过ajax提交{id,p} 这2个参数,就可以了,页面返回json的url参数中原样带上p即可. 2.目的2: 步骤1:在如下页面 ...

  5. 【前台 ajax】web项目前台传递数组给后台 两种方式

    项目使用maven    springMVC 有需求 将前台的数组   在ajax中 送给后台 方式1: 前台代码:[注意:ajax中的属性---traditional:true,  ] 如果Post ...

  6. jquery ajax提交表单数据的两种方式

    http://www.kwstu.com/ArticleView/kwstu_201331316441313 貌似AJAX越来越火了,作为一个WEB程序开发者要是不会这个感觉就要落伍,甚至有可能在求职 ...

  7. ajax的data传参的两种方式

    ajax的data传参的两种方式 本文为转载. 1.[javascript] view plain copy /** * 订单取消 * @return {Boolean} 处理是否成功 */ func ...

  8. SpringMVC实现Action的两种方式以及与Struts2的区别

    4.程序员写的Action可采用哪两种方式? 第一.实现Controller接口第二.继承自AbstractCommandController接口 5.springmvc与struts2的区别? 第一 ...

  9. egg.js 通过 form 和 ajax 两种方式上传文件并自定义目录和文件名

    egg.js 通过 form 和 ajax 两种方式上传文件并自定义目录和文件名 评论:10 · 阅读:8437· 喜欢:0 一.需求 二.CSRF 校验 三.通过 form 表单上传文件 四.通过 ...

随机推荐

  1. wepack+sass+vue 入门教程(一)

    一.安装node.js node.js是基础,必须先安装.而且最新版的node.js,已经集成了npm. 下载地址 node安装,一路按默认即可. 二.全局安装webpack npm install ...

  2. Android总结之链式调用(方法链)

    前言: 最近在学习总结Android属性动画的时候,发现Android的属性动画设计采用了链式调用的方式,然后又回顾了一下了以前接触的开源框架Glide也是采用链式调用的方式,还有最近火的一塌糊涂的R ...

  3. MVC常遇见的几个场景代码分享

    本次主要分享几个场景的处理代码,有更好处理方式多多交流,相互促进进步:代码由来主要是这几天使用前端Ace框架做后台管理系统,这Ace是H5框架里面的控件效果挺多的,做兼容也很好,有点遗憾是控件效果基本 ...

  4. iOS逆向工程之App脱壳

    本篇博客以微信为例,给微信脱壳."砸壳"在iOS逆向工程中是经常做的一件事情,,因为从AppStore直接下载安装的App是加壳的,其实就是经过加密的,这个“砸壳”的过程就是一个解 ...

  5. [转载]Cookie/Session的机制与安全

    Cookie和Session是为了在无状态的HTTP协议之上维护会话状态,使得服务器可以知道当前是和哪个客户在打交道.本文来详细讨论Cookie和Session的实现机制,以及其中涉及的安全问题. 因 ...

  6. 根据ip判断返回城市名称查询当地天气

    <?phpheader("content-type:text/html;charset=utf-8");date_default_timezone_set("Asi ...

  7. 装饰者模式 Decoration

    1.什么是装饰者模式 动态给对象增加功能,从一个对象的外部来给对象添加功能,相当于改变了对象的外观,比用继承的方式更加的灵活.当使用装饰后,从外部系统的角度看,就不再是原来的那个对象了,而是使用一系列 ...

  8. springmvc 多数据源 SSM java redis shiro ehcache 头像裁剪

    获取下载地址   QQ 313596790  A 调用摄像头拍照,自定义裁剪编辑头像 B 集成代码生成器 [正反双向](单表.主表.明细表.树形表,开发利器)+快速构建表单;  技术:31359679 ...

  9. 使用Git Bash远程添加分支和简单部署你的静态页面

    新建一个分支:git branch mybranch(mybranch你的分支名字) 切换到你的新分支: git checkout mybranch 将新分支发布在github上: git push ...

  10. 树莓派 基于Web的温度计

    前言:家里的树莓派吃灰很久,于是拿出来做个室内温度展示也不错. 板子是model b型. 使用Python开发,web框架是flask,温度传感器是ds18b20 1 硬件连接 ds18b20的vcc ...