日期:2014-05-16  浏览次数:20429 次

关于strurts2和json的问题
struts2中返回json对象,以注入字符串result为例

配置sturts2 文件代码:

<result type="json">
<param name="includeProperties">result</param>
</result>


java 中代码:
方法一:
Gson gson = new Gson();

this.result = gson.toJson(list);


方法二:
List list = new ArrayList();

for (java.util.Iterator iterator = listTopic.iterator();

iterator.hasNext();){
      
TimingSchedule ts = (TimingSchedule) iterator.next();
       
JSONObject obj = JSONObject.fromObject(ts);

list1.add(obj);

JSONArray json = JSONArray.fromObject(list);

this.result = json.toString();


js中代码

function  showlist(date){
   
$.ajax({
           type : "post",
          
    url : "<%=request.getContextPath()%>/framework/scheduleview_listTopic.do?date="+date+"&suiji"+new Date().getTime(),

    cache : true,
     
    success : function(data,textStatus){

          //重要代码
            if(eval("tiltles="+data.result)){
    
             eval("tiltles="+data.result);
    
     var str="";
   
     if(tiltles.length==0){
    
             str+="<tr class='SpecialRows2' width='100%'><td align='center' colspan='20'>日程</td></tr>";
            
  $("#showResult").html(str);
 
return;    
     }
    
           for(i=0;i<tiltles.length;i++){
          
str+="<tr class='GeneralRows'>"
            +"<td align='center'  width='15%' ><a href=''javascript:ss()''> "+tiltles[i].startdate+"</a></td>"
            +"<td align='center'  border='1px'>"+tiltles[i].title+"</td>"
                        +"</tr>"
                                  
}
         $("#showResult").html(str);

                }                
    },
                     error : function(XMLHttpRequest, textStatus, errorThrown){
                        alert("服务器出错,请稍候再进行登录");
   &