java中ajax使用--传回一个字符串

1--jsp页面调用的js函数
//检查用户名是否存在
function validateUserName()
{
  var loginName = $("#userName").val();
$.ajax( {
type : "post",
url : "validateUserName.action",
dataType : "json",
data : {"loginName":loginName},
async : false,
cache : false,
success : function(msg) {
var objInfo = msg.jsonResultMsg;
if(objInfo == "true")
{

$("#userName").val('');
$("#userName").focus();
alert("用户名已存在,请更换用户名!");
}
}
});
}


2--xml配置



jsonResultMsg



3--XXaction.action
// 判断用户名是否已存在
public String validateUserName() {
try {
String loginName = getRequest().getParameter("loginName");
String Hql = "from TUserName t where t.userName = '" + loginName
+ "' ";
int count = this.sealbusinessService.GetCount(Hql);
if (count > 0) {
jsonResultMsg = "true";
} else {
jsonResultMsg = "false";
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}


private String jsonResultMsg = null;
public String getJsonResultMsg() {
return jsonResultMsg;
}




public void setJsonResultMsg(String jsonResultMsg) {
this.jsonResultMsg = jsonResultMsg;
}

你可能感兴趣的:(java,web)