Ajax 请求时莫明执行其它未调用的 Action 的原因及解决方式

最近在一个项目中使用了 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;
	}
}



你可能感兴趣的:(json,Ajax,annotation,struts2)