您好,登錄后才能下訂單哦!
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文件路徑:
<script language="javascript" src="scripts/javascript/jquery-1.7.js"></script>
<script language="javascript" src="scripts/javascript/json2.js"></script>
-----------------------------------------------------------默默無聞的分割線-----------------------------------------------------------
js函數:
function checkUserName(){
var url='checkUserName';
var params={
studentName:'1'
};
$.post(
url,
params,
function callback(json){
var responseData = eval(json);
var result = responseData.ckUserName;
if(result=="pass"){
$("h2").text("pass");
}else{
$("h2").text("npass");
}
},
'json'
);
}
-----------------------------------------------------------默默無聞的分割線-----------------------------------------------------------
配置Struts2.xml:
<action name="checkUserName" class="com.shenzhen.management.action.HelloWorldAjaxAction" method = "checkUserName">
<result type="json">
<param name="#root">ckUserName</param>
</result>
</action>
-----------------------------------------------------------默默無聞的分割線-----------------------------------------------------------
Action方法(通過Spring注入Service接口拋異常,所以另外寫了一個HelloWorldAjaxAction.java文件):
public String checkUserName()
{
String retStr = "fail";
//通過Spring注入Service接口拋異常,未找到原因,現通過ApplicationContext獲取bean,知道如何解決的大神請留言,謝謝!
ServletContext servletContext = ServletActionContext.getServletContext();
ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
StudentService studentService = (StudentService)applicationContext.getBean("studentService");
try{
ckUserName = "pass";
retStr = "success";
}catch(Exception e){
ckUserName = "npass";
}
return retStr;
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。