`
joeyhacker
  • 浏览: 94481 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

当struts2遇上json.

阅读更多

这几天想用jquery做点东西,  由于我的后台架构是SSH2的, 所以难免要用jquery和struts2打交到.  当然他们直接的交流语言首选当然是json了. 由于以前做过extjs的项目, 所以对json的数据结构还是比较了解地,呵呵.

开始步入正题:  先说说web端吧, jquery的ajax是支持json格式的, 例如:

$.getJSON("test.js", function(json){
  alert("JSON Data: " + json.users[3].name);
});

当然你也可以用$.get ,

 $.get("test.php", function(data){
  alert("Data Loaded: " + data);
}

,"json"

); 

最后要指明数据类型. 具体的看api吧. 看看我的小例子:

$.get("manage/test.action" ,function(json){

var data = eval('('+json+')'); //转换成javascript类型的对象.

alert(data.xxx);

//自己做处理吧. 格式都是{"xxx":["xxx":"xxx","xxx":"xxx"]} 的样子....

},"json";

}

 

其他就没啥了, 有人说要导入json.js那个文件. 我认为没用. 反正我没用上那个文件里面的方法.

看看struts2 , 我们都知道有个jar包叫"json-lib.jar", 好像是google_code上的~ .  如果用他呢, 会把你累死~ , 以前我做extjs的时候就是用的它, 哎~ 一个字累. JSONObject 和 JSONArray 来回套着用..  代码N多.. 

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.joey.shopping.entity.shop.SuperType;

public class TestAction extends GoodsAction {

	private static final long serialVersionUID = 1L;
	private List<SuperType> tl;


	public List<SuperType> getTl() {
		return tl;
	}

	public String execute() throws Exception {
		
		this.tl = goodsManager.getAllS_Types();
		return SUCCESS;
		List<SuperType> list = new ArrayList<SuperType>();
		JSONObject json = new JSONObject();
		JSONArray st_list = new JSONArray();
		list = goodsManager.getAllS_Types();
		int j = 0;
		if (list.size() == 0) {
			json.put("types", null);
			return;
		}
		json.put("count", list.size());
		Iterator<SuperType> it = list.iterator();
		while (it.hasNext()) {
			int i = 0;
			JSONObject st1 = new JSONObject();
			JSONArray t_list = new JSONArray();
			SuperType st = it.next();
			Iterator<Type> tt = st.getTypes().iterator();
			while (tt.hasNext()) {
				JSONObject t1 = new JSONObject();
				Type t = tt.next();
				t1.put("t_id", t.getId());
				t1.put("t_name", t.getName());
				t1.put("t_info", t.getInfo());
				t1.put("g_size", t.getGoods().size());
				t_list.add(i++, t1);
			}
			st1.put("st_id", st.getId());
			st1.put("st_name", st.getSt_name());
			st1.put("st_info", st.getSt_info());
			st1.put("subtypes", t_list);
			st_list.add(j++, st1);
		}
		json.put("types", st_list);
		this.renderJson(json.toString());
	}

}

 

当然现在好了, 我们的struts2有个json的插件, 也是在google上.名字叫jsonplugin. 有个它我们可以直接返回list类型的数据哦, 它自动帮你转换成json,呵呵.  来看看struts2的配置. 本人强烈讨厌用.xml配东西,感觉特累.  看看我的写法.

@ParentPackage("json-default")
public class TestAction extends GoodsAction { 

 private static final long serialVersionUID = 1L;
 private List<SuperType> tl;


 public List<SuperType> getTl() {
  return tl;
 }

 @Action(value = "/test", results = { @Result(name = "success", type = "json") })
 public String execute() throws Exception {

 this.tl = goodsManager.getAllS_Types();
  return SUCCESS;

   }

}

 

 

 

@GoodsAct

ion可以用其他的package.但你的这个类必须用json-default这个包

很简单吧, 你要是用.xml, 它前面那一堆东西你能背着写?? 呵呵.  看清楚哦, 我返回的是list类型的数据. 不过没关系, struts2会帮你转成你想要的. 先写到这吧, 等想起来了再说.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics