struts中实现ajax的配置信息
2021-07-08 18:04
标签:servlet com 方法 package turn 商品 ltm 技术分享 efault 基于Struts2框架下实现Ajax有两种方式,第一种是原声的方式,另外一种是struts2自带的一个插件。 js部分调用方式是一样的: JS代码: 第一种原生的实现方式: Action中创建一个方法: struts.xml中配置: 这种方式不太推荐使用。 这里重点讲解下第二种方式: 1.引入struts2-json-plugin-2.5.2.jar 2.Action中添加类似的如下代码: Struts.xml中配置如下: JS中接受返回结果: 返回JSON格式: JS解析如下: struts中实现ajax的配置信息 标签:servlet com 方法 package turn 商品 ltm 技术分享 efault 原文地址:http://www.cnblogs.com/hmpcly/p/7096469.html 1 function testAjax() {
2
3 var $userNameInput = $("#ajax_username");
4 var userName = $userNameInput.val();
5
6 $.ajax({
7 url : "originAjax.action",
8 type : "GET",
9 data : "ajaxField=" + userName,
10 success : function(data, textStatus) {
11 alert(data);
12 }
13 });
14 }
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();
}
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
*
* */
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>
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">trueparam>
param name="root">resultparam>
result>
action>
package>
struts>
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
文章标题:struts中实现ajax的配置信息
文章链接:http://soscw.com/index.php/essay/102434.html