最近在一個項目中使用了 Ajax ,後臺代碼採用了 Annotation 的方式進行註解,爲了方便,我把相關的 Ajax 請求的 Action 方法都封到了一個類裏面,如下:
@Controller
@Namespace(value = "/session")
@Scope("prototype")
@ParentPackage("json-default")
public class AjaxAction extends ActionSupport {
// 此處省略屬性的定義及 get/set 方法
/**
* 根據用戶名檢測用戶是否需要驗證碼
*
* @return
*/
@Action(value = "isneedcapcha", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
public String checkNeedCapcha() {
// 省略方法具體業務邏輯
return SUCCESS;
}
@Action(value = "delserversession", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
public String delServerSessionBySid() {
// TODO Auto-generated method stub
// 省略方法具體業務邏輯
return SUCCESS;
}
@Action(value = "getallserversessionmodel", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
public String getAllServerSession() throws Exception {
// TODO Auto-generated method stub
// 省略方法具體業務邏輯
return SUCCESS;
}
}
當我調用到 isneedcapcha 這個 Action 時,我發現 getallserversessionmodel 對應的方法也被執行了,但我排查了很久,getallserversessionmodel 這個 Action 我並沒有去調用啊,而且當我調用 delserversession 時,getallserversessionmodel 一樣也被調用了。但是當我把 getallserversessionmodel 獨立成一個類時,這個問題就有莫明奇妙地解決了。獨立出來後的類如下:
@Controller
@Namespace(value = "/session")
@Scope("prototype")
@ParentPackage("json-default")
public class AjaxAction1 extends ActionSupport {
// 此處省略屬性的定義及 get/set 方法
/**
* 根據用戶名檢測用戶是否需要驗證碼
*
* @return
*/
@Action(value = "isneedcapcha", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
public String checkNeedCapcha() {
// 省略方法具體業務邏輯
return SUCCESS;
}
@Action(value = "delserversession", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
public String delServerSessionBySid() {
// TODO Auto-generated method stub
// 省略方法具體業務邏輯
return SUCCESS;
}
}
@Controller
@Namespace(value = "/session")
@Scope("prototype")
@ParentPackage("json-default")
public class AjaxAction2 extends ActionSupport {
// 此處省略屬性的定義及 get/set 方法
@Action(value = "getallserversessionmodel", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
public String getAllServerSession() throws Exception {
// TODO Auto-generated method stub
// 省略方法具體業務邏輯
return SUCCESS;
}
}
雖然問題就這樣勉強消失了,但這個問題還是讓人納悶,後來查了資料,發現在 Struts2 中使用 json 插件時, Action 類中所有的以"get"開頭的方法默認都會被序列化,要避免方法被序列化,可以修改方法名不用“get”開頭,或者在方法上加上註解@JSON(serialize = false)聲明不要序列化,問題解決,如下:
@Controller
@Namespace(value = "/session")
@Scope("prototype")
@ParentPackage("json-default")
public class AjaxAction extends ActionSupport {
// 此處省略屬性的定義及 get/set 方法
/**
* 根據用戶名檢測用戶是否需要驗證碼
*
* @return
*/
@Action(value = "isneedcapcha", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
@JSON(serialize = false)
public String checkNeedCapcha() {
// 省略方法具體業務邏輯
return SUCCESS;
}
@Action(value = "delserversession", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
@JSON(serialize = false)
public String delServerSessionBySid() {
// TODO Auto-generated method stub
// 省略方法具體業務邏輯
return SUCCESS;
}
@Action(value = "getallserversessionmodel", results = { @Result(name = "success", type = "json", params = {
"noCache", "true", "contentType", "text/html" }) }, interceptorRefs = {
@InterceptorRef(value = "defaultStack"),
@InterceptorRef(value = "json") })
@JSON(serialize = false)
public String getAllServerSession() throws Exception {
// TODO Auto-generated method stub
// 省略方法具體業務邏輯
return SUCCESS;
}
}