2012-02-06 32 views
0

返回值,我做我的支柱項目現在我想要檢索的數據爲JSON。但是我想獲得json響應的變量不會檢索我需要的json值。Struts2的JSON不從變量

這裏是我的Action類

import com.myDrDirect.hbmobj.Doctor; 
import com.myDrDirect.order.dao.DoctorDashBoardDao; 
import com.opensymphony.xwork2.ActionContext; 
import com.opensymphony.xwork2.ActionSupport; 
import java.util.List; 
import java.util.Map; 

public class patientOrderDetails extends ActionSupport { 
    private int rows; 
    private int total; 
    private int records; 
    private int page; 
    private List recentOrders; 
    public String jsonRecentOrderDetails() { 

     try{ 
      Map sessionSingleDoctor = ActionContext.getContext().getSession(); 
      Doctor SessionObj = (Doctor) sessionSingleDoctor.get("Doctor"); 

      records = DoctorDashBoardDao.getInstance().getCount(); 
      //records = 10; 
      setRows(Integer.parseInt(getText("number.rows.per.page"))); 
     //rows = 10; 
      int to = (getRows() * getPage()); 
      int from = (getRows() * (getPage()-1)); 


      total =(int) Math.ceil((double)records/(double)rows); 
      recentOrders= DoctorDashBoardDao.getInstance().getDoctorRecentOrderDetails(rows,from,SessionObj.getId()); 


    } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println(e.getCause()); 
      //log.error("Error occured " + e.toString()); 
      return ERROR; 
     } 
      return SUCCESS; 
    } 

    public int getRows() { 
     return rows; 
    } 

    public void setRows(int rows) { 
     this.rows = rows; 
    } 

    public int getPage() { 
     return page; 
    } 

    public void setPage(int page) { 
     this.page = page; 
    } 

    public int getRecords() { 
     return records; 
    } 

    public void setRecords(int records) { 
     this.records = records; 
    } 

    public int getTotal() { 
     return total; 
    } 

    public void setTotal(int total) { 
     this.total = total; 
    } 

    public List getRecentOrders() { 
     return recentOrders; 
    } 

    public void setRecentOrders(List recentOrders) { 
     this.recentOrders= recentOrders; 
    } 


} 

和我在struts.xml有

<package name="json" extends="json-default"> 

      <action name="DashBoardActivityJson" class="com.myDrDirect.doctor.action.patientOrderDetails" method="jsonRecentOrderDetails"> 
       <result name="success" type="json"> 
        <!-- serialize the result property of the action -->      
        <param name="includeProperties">recentOrders</param> 

       </result> 
      </action> 
      <action name="grid" > 
       <result name="success" >/WEB-INF/doctor/jsonGrid.jsp</result> 
      </action> 
     </package> 

我需要在RecentOrders1 varable響應,但這個變量無法檢索JSON數據。當我檢索變量records我得到了正確的答覆。記錄只有列表的總數,但recentOrder1返回一個列表。我需要在我的查看頁面中將recentOrder作爲列表。任何人都可以告訴我在上述操作或XML文件中有什麼問題。請幫忙。

有一件事我已經在這裏注意到的是在recentOrder返回值是一個列表。我是否需要將此列表轉換爲json字符串。是否有必要或struts2自動更改我的列表變量爲JSON字符串?

回答

0

您的行爲不具有財產RecentOrders1,只有recentOrders

注意兩者的資本化,以及缺乏一個數字的。

+0

感謝您的即時回覆。我已將變量名稱** RecentOrders1 **更改爲** recentOrders **,併爲此寫入getter和setter。我也改變了我的struts.xml結果類型像這樣** recentOrders **。但我無法查看結果。結果顯示結果略有變化,如{「recentOrders」:[]}。可能是什麼問題 – user359187 2012-02-06 11:41:57

+2

這是一個空的列表。確認它有內容。 – 2012-02-06 11:46:05

+0

當我打印recentOrders控制檯打印這樣的結果[email protected] [email protected] [email protected] [email protected] com。 [email protected] [email protected] [email protected] [email protected] [email protected] com.myDrDirect.hbmobj.PatientOrder @ 1868e75 [email protected]。數據已存在,但在查看頁面中無法看到 – user359187 2012-02-06 12:34:21