jquery.post()

jar包:

json-lib-2.2.3-jdk15.jar

struts2-json-plugin-2.3.4.1.jar(此包版本必须和Struts2版本对应,否则会抛错)

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

js文件:

query-1.7.js和json2.js。

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

页面导入js文件路径:

<scriptlanguage="javascript"src="scripts/javascript/jquery-1.7.js"></script>

<scriptlanguage="javascript"src="scripts/javascript/json2.js"></script>

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

js函数:

functioncheckUserName(){

varurl='checkUserName';

varparams={

studentName:'1'

};

$.post(

url,

params,

functioncallback(json){

varresponseData=eval(json);

varresult=responseData.ckUserName;

if(result=="pass"){

$("h1").text("pass");

}else{

$("h1").text("npass");

}

},

'json'

);

}

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

配置Struts2.xml:

<actionname="checkUserName"class="com.shenzhen.management.action.HelloWorldAjaxAction"method="checkUserName">

<resulttype="json">

<paramname="#root">ckUserName</param>

</result>

</action>

-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------

Action方法(通过Spring注入Service接口抛异常,所以另外写了一个HelloWorldAjaxAction.java文件):

publicStringcheckUserName()

{

StringretStr="fail";

//通过Spring注入Service接口抛异常,未找到原因,现通过ApplicationContext获取bean,知道如何解决的大神请留言,谢谢!

ServletContextservletContext=ServletActionContext.getServletContext();

ApplicationContextapplicationContext=WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

StudentServicestudentService=(StudentService)applicationContext.getBean("studentService");

try{

ckUsername="pass";

retStr="success";

}catch(Exceptione){

ckUsername="npass";

}

returnretStr;

}

相关推荐