struts2 默认包 + jquery 实现ajax(JSON)Struts2+JQuery+JSON集成: struts2支持JSON,有专门的插件jsonplugin。网上对于jsonplugin以及相关集成,有很详细的说明,这里也不再叙述。主要说一下struts2默认包的相关集成:
由于时间关系,没来得及整理,先贴一些关键代码:
- /**
- *@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);
- }
- );
- }
/** *@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函数。修改后的代码:
- /**
- *@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);
- }
- );
- }
/** *@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配置代码:
<action name="busi***" > </action>
相关action代码:
- //…..省略相关的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;
- }
//.....省略相关的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; }