【问题标题】:struts2 json do not return value from a variablestruts2 json不从变量返回值
【发布时间】:2012-02-06 11:11:18
【问题描述】:

我正在做我的 struts 项目,现在我想以 json 格式检索数据。但是我想要获取 json 响应的变量并没有检索到我需要的 json 值。

这是我的动作类

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 变量中获取响应,但此变量不检索 json 数据。当我检索变量records 时,我得到了正确的响应。记录者只有列表的总数,但 recentOrder1 返回一个列表。我需要在我的视图页面中获取recentOrder 作为列表。谁能告诉我上述操作或 xml 文件中的问题是什么。请帮忙。

我在这里注意到的一件事是recentOrder 中的返回值是一个列表。我需要将此列表转换为 json 字符串吗?有必要还是struts2自动把我的list变量改成json字符串?

【问题讨论】:

    标签: json struts2 struts struts-config


    【解决方案1】:

    您的操作没有属性RecentOrders1,只有recentOrders

    注意大写和缺少数字。

    【讨论】:

    • 感谢您的即时回复。我已将变量名称 RecentOrders1 更改为 recentOrders 并为此编写 getter 和 setter。我也像这样 recentOrders 更改了我的 struts.xml 结果类型。但我无法查看结果。现在的结果略有变化,结果显示为 {"recentOrders":[]}。可能是什么问题
    • 这是一个空列表。验证它有内容。
    • 当我打印 recentOrders 时,控制台会打印出类似 com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect 的结果。 hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1aa89eb com.myDrDirect.hbmobj.PatientOrder@1868e75 com.myDrDirect.hbmobj.PatientOrder@1aa89eb。数据在那里,但在视图页面中看不到
    • @user359187 使用 Firebug/Chrome 调试工具验证结果;看看电线上回来的是什么。您还可以使用"root" 参数将列表设置为唯一返回的对象。
    • @user359187 不,JSON 插件序列化对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2011-12-27
    相关资源
    最近更新 更多