struts2 默认包 + jquery 实现ajax(JSON)



struts2 默认包 + jquery 实现ajax(JSON)Struts2+JQuery+JSON集成: struts2支持JSON,有专门的插件jsonplugin。网上对于jsonplugin以及相关集成,有很详细的说明,这里也不再叙述。主要说一下struts2默认包的相关集成:
由于时间关系,没来得及整理,先贴一些关键代码:

Java代码 复制代码 收藏代码
  1.     /**
  2. *@author:   steve lee   2009-9-03下午18:42
  3. *ajaxrequest主要是向后台请求数据,该数据是符合JSON规范的字符串
  4. 量provinceStrValue 通过属性data,向后台传递参数
  5. ***/
  6. function ajaxrequest(){
  7.      var provinceStrValue = document.getElementById(‘provinceStrForm’).value;
  8.        $.ajax({
  9.   type: “POST”,
  10.   url: “busi***”,
  11.   data:”provinceStrForm=” + provinceStrValue,
  12.                        [b][color=red] dataType: “json”,[/color][/b]
  13.   success: function(receivedRes){
  14.   alert(“received    ” + receivedRes);
  15.   }
  16. );
  17. }
  	 	/**
 	 	*@author:	steve lee 	2009-9-03下午18:42
 	 	*ajaxrequest主要是向后台请求数据,该数据是符合JSON规范的字符串
		*变量provinceStrValue 通过属性data,向后台传递参数
 	 	***/
 	 	function ajaxrequest(){
 	 		 var provinceStrValue = document.getElementById('provinceStrForm').value;
 	 		   $.ajax({
			   type: "POST",
			   url: "busi***",
			   data:"provinceStrForm=" + provinceStrValue,
                           [b][color=red] dataType: "json",[/color][/b]
			   success: function(receivedRes){
			   alert("received    " + receivedRes);
			   }
			}); 
 	 	}

上述代码就是jquery ajax请求方式的其中一种形式:对于红色部分代码: dataType: “json”,,最好去掉,因为如果在服务端代码中如果没有写response的contentType为text/x-json,将会出错。 去掉了dataType: “json”,,要获得JSON对象,还得使用eval函数。修改后的代码:

Java代码 复制代码 收藏代码
  1.     /**
  2. *@author:   steve lee   2009-9-03下午18:42
  3. *ajaxrequest主要是向后台请求数据,该数据是符合JSON规范的字符串
  4. 量provinceStrValue 通过属性data,向后台传递参数
  5. ***/
  6. function ajaxrequest(){
  7.      var provinceStrValue = document.getElementById(‘provinceStrForm’).value;
  8.        $.ajax({
  9.   type: “POST”,
  10.   url: “busi***”,
  11.   data:”provinceStrForm=” + provinceStrValue,
  12.   success: function(receivedRes){
  13.                        var objJson = eval(“(” + receivedRes +”)”);
  14.   alert(“received    ” + objJson .length);
  15.   }
  16. );
  17. }
  	 	/**
 	 	*@author:	steve lee 	2009-9-03下午18:42
 	 	*ajaxrequest主要是向后台请求数据,该数据是符合JSON规范的字符串
		*变量provinceStrValue 通过属性data,向后台传递参数
 	 	***/
 	 	function ajaxrequest(){
 	 		 var provinceStrValue = document.getElementById('provinceStrForm').value;
 	 		   $.ajax({
			   type: "POST",
			   url: "busi***",
			   data:"provinceStrForm=" + provinceStrValue,
			   success: function(receivedRes){
                           var objJson = eval("(" + receivedRes +")");
			   alert("received    " + objJson .length);
			   }
			}); 
 	 	}

相关struts-busi***.xml配置代码:

Xml代码 复制代码 收藏代码
  1. <action name=”busi***” class=”busi***Action” >
  2. </action>
   <action name="busi***" >

   </action>

相关action代码:

Java代码 复制代码 收藏代码
  1. //…..省略相关的service
  2.         private String provinceStrForm ;
  3.     public String getProvinceStrForm() {
  4.         return provinceStrForm;
  5.     }
  6.     public void setProvinceStrForm(String provinceStrForm) {
  7.         this.provinceStrForm = provinceStrForm;
  8.     }
  9.         //……省略getJsonString();该方法是组合JSON格式的字串,符合JSON规范和业务逻辑即可
  10.         public String execute() throws Exception {
  11.         // TODO Auto-generated method stub
  12.         String outStr = getJsonString(this.getProvinceStrForm());
  13.         HttpServletResponse response = (HttpServletResponse)
  14.                        ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
  15.         response.setCharacterEncoding(Constant.ENCODE_UTF8);//Constant.ENCODE_UTF8  相当于”utf-8″
  16. //如果原先js里声明dataType属性为json,那么在此处添加以下代码:
  17.                 //response.setContentType(“text/x-json”);
  18.                 //js里面的eval函数亦没有必要了
  19.         response.getWriter().write(outStr);
  20.         response.getWriter().flush();
  21.         response.getWriter().close();
  22.         return null;
  23.     }
//.....省略相关的service
        private String provinceStrForm ;

	public String getProvinceStrForm() {
		return provinceStrForm;
	}

	public void setProvinceStrForm(String provinceStrForm) {
		this.provinceStrForm = provinceStrForm;
	}

        //......省略getJsonString();该方法是组合JSON格式的字串,符合JSON规范和业务逻辑即可

        public String execute() throws Exception {
		// TODO Auto-generated method stub

		String outStr = getJsonString(this.getProvinceStrForm());
		HttpServletResponse response = (HttpServletResponse)
		               ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
		response.setCharacterEncoding(Constant.ENCODE_UTF8);//Constant.ENCODE_UTF8  相当于"utf-8"

//如果原先js里声明dataType属性为json,那么在此处添加以下代码:
                //response.setContentType("text/x-json");
                //js里面的eval函数亦没有必要了
		response.getWriter().write(outStr);
		response.getWriter().flush();
		response.getWriter().close();

		return null;
	}