struts2 json不从变量返回值

发布于 2025-01-02 10:48:04 字数 3169 浏览 7 评论 0原文

我正在做我的 struts 项目,现在我想以 json 形式检索数据。但我想要获取 json 响应的变量无法检索我需要的 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 变量中获取响应,但该变量不检索 json 数据。当我检索变量records时,我得到了正确的响应。记录只有列表的总计数,但最近的订单1返回一个列表。我需要在我的视图页面中获取最近的订单列表。任何人都可以告诉我上面的操作或 xml 文件中有什么问题吗?请帮忙。

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

I am doing my struts project now i want to retrieve data as json. But the variable that i want to get the json response does'nt retrieve the json values that i need.

Here is my Action class

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;
    }


}

and my struts.xml have

<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>

I need to get the response in RecentOrders1 varable but this variable not retrieve the json data. When i retrieve the variable records i got the response correctly. The recorde only have a totalcount of the list but the recentOrder1 returns a list. I need to get the recentOrder as list in my view page. Can anybody please tell me what is the issue in the above action or xml file. Please help.

One thing i have noticed here is return value in recentOrder is a list. Do i need to convert this list to json string. Is it necessary or struts2 automatically change my list variable into a json string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

油焖大侠 2025-01-09 10:48:04

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

请注意大小写和缺少数字。

Your action doesn't have a property RecentOrders1, only recentOrders.

Note both the capitalization, and lack of a digit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文