【问题标题】:How to return the json values as database object in struts hibernate如何在struts hibernate中将json值作为数据库对象返回
【发布时间】:2012-03-06 14:39:42
【问题描述】:

我回来了一件更复杂的事情。这次我不知道我的逻辑是否正确。

我必须将数据库中的数据作为 json 对象返回。在这个 json 对象中,我返回了一些值,这些值是数据库中其他表的外键。我从数据库中正确获取了值,现在我想使用 struts 迭代标记从我的 ajax 成功中迭代 forien 键值。如果我们可以迭代对象的 json 值,则可以进行迭代,如果我是正确的,请任何人分享这个想法。

<s:iterator value="listdoctorProducts" status="st" >
            <div class="productListing">
                <h3><span><s:property value="inventory.name" /></span> - <s:property value="inventory.description" /> </h3>
                    <div class="productDtlLeft">
                        <s:iterator value="inventory.pronovaInventories" >
                            <img src="images/pronova/products/<s:property value="image" />" width="31" height="94" alt="" />
                        </s:iterator>                        
                    </div>     
                    <s:iterator value="inventory.productRatings" status="st">
                        <s:if test="(#st.index+1)==1">
                            <s:set var="no2" value="rating" /> 
                        </s:if>
                        <s:else>
                            <s:set var="no2" value="#no2+rating" /> 
                        </s:else>
                        <s:set var="count" value="#st.index+1" /> 
                    </s:iterator>
                     <s:set var="aver" value="%{#no2*10/#count}"/>
                     <s:set var="result" value="#aver*1.0/10"/>
                    <div class="productDtlRight">
                        <div class="ratting">
                            <div class="rattingStar">bg</div>
                            <div class="rattingOrange" style="width:<s:property value="(#result/5)*100"/>%;">bg</div>
                        </div>

                        <span>(<s:property value="#aver*1.0/10"/>)</span>
                        <div class="clear"></div>
                        <s:set var="no2" value="0" /> 
                        <s:bean name="com.zoondia.common.calculationBean" var="decCalBeansuggestedPrice">
                            <s:param name="valueOne" value="retailPrice"/>                                                             
                            <s:param name="decimalPlace">#.##</s:param>
                        </s:bean>
                        <h4>$<s:property value="#decCalBeansuggestedPrice.decimalPointConversionResult" /></h4>
                        <a class="addtoCart" href="javaScript:void(0)">View All</a>
                   </div>
                   <div class="clear"></div>
                </div>
        </s:iterator> 

在这个 jsp 页面中,您可以看到我在 listdoctorProducts 中有一个数据库对象列表,我可以通过像这样访问它来获取 productRating 表的值

<s:iterator value="inventory.productRatings" status="st">
                            <s:if test="(#st.index+1)==1">
                                <s:set var="no2" value="rating" /> 
                            </s:if>

我在此处列出的此代码用于第一页,如果用户单击第二页,我必须为用户列出第二页,我们需要通过填充 json 值在 ajax 中执行此操作。我的问题是,如果我将响应作为 ajax 检索,我怎么能编写这样的迭代器?

这是我的动作类

public String getEcommerceWidgetFourJson(){
        try{            
            Doctor DtObj = null;
            Map sessionSingleDoctor = ActionContext.getContext().getSession();
            Object Obj = null;
            Obj = sessionSingleDoctor.get("Doctor");
            DtObj = (Doctor) Obj;
            int totalCount = DoctorDao.getInstance().totalNumberOfdoctorToProductsForJson(DtObj.getId());
            numberOfRowsPerPage = Integer.parseInt(getText("ecommerce.widget.product.list.four"));
            totalNumberOfRows = (int)Math.ceil((float)totalCount/numberOfRowsPerPage);
            if(pageNum < 1){
                pageNum = 1;
            }else if(pageNum > lastRows){
                pageNum = lastRows;
            }
            listdoctorProducts = DoctorDao.getInstance().getDoctorProductsAsJson(DtObj.getId(),numberOfRowsPerPage,pageNum);



        }catch(Exception e){
            e.printStackTrace();
        }
        return SUCCESS;
    }

我这里没有添加变量声明和setter和getter。这是我从数据库访问数据的 Dao 函数

public List<DoctorToProducts> getDoctorProductsAsJson(int DocId,int numberOfRowsPerPage, int pageNum){
        List<DoctorToProducts> dp = null;
        DoctorToProducts ldp = null;
        SessionFactory sessionFactory =
                    (SessionFactory) ServletActionContext.getServletContext().getAttribute(HibernateListener.KEY_NAME);
        Session Hibernatesession = sessionFactory.openSession();
        Hibernatesession.beginTransaction(); 
        Query  q = (Query) Hibernatesession.createQuery("select id,inventory.id,doctor.id,inventory.pronovaInventories,retailPrice,unitCreditValue from DoctorToProducts where doctor.id="+DocId);
        q.setFirstResult((pageNum-1)*numberOfRowsPerPage);
        q.setMaxResults(numberOfRowsPerPage);
        dp = q.list();
        Hibernatesession.getTransaction().commit();
        Hibernatesession.flush();
        return dp;
    }

而我的 struts.xml 是

<action name="frPdtListPagination" class="com.zoondia.action.DoctorPdtsPagination" method="getEcommerceWidgetFourJson">
            <result  type="json">                

            </result>
        </action>

只需为此 json 调用添加操作即可。

【问题讨论】:

  • 显示一些代码,如果您尝试过任何方法,您遇到的问题到底是什么。
  • 你是使用注解还是xml来指定动作?如何显示 xml(如果有)和操作。精简版的动作会很棒,但可能没有必要。
  • 我已经添加了代码,请检查它

标签: json hibernate struts2


【解决方案1】:

这将是我的猜测工作......

您的问题的简短回答是:不。那就是 不能同时返回 json 和迭代你的动作中的对象,因为它的设置方式产生了有意义的结果。

让我们更改设置,让答案变为肯定。这是未测试的快速版本:

//Not tested, this is just an example of what you can do
package com.quaternion.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

public class DoctorActions extends ActionSupport implements Preparable {
    @Autowired DoctorService doctorService; //injected from spring
    List<String> doctorProducts;

    @Override
    public void prepare() {
        doctorProducts = doctorService.getProducts();
    }

    @Action(results = {
        @Result(type = "json", params = {
            "includeProperties",
            "doctorProducts"})
    })
    public String DoctorJsonProducts() {
        return SUCCESS;
    }

    @Action
    public String DoctorProducts(){
        return SUCCESS;
    }
}

上面的解释:

通过让我们的 Action 实现 Preparable,struts2 将调用使用医生服务集医生产品的准备方法。请注意,当与 Doctor 管理有关的所有内容(包括事务管理)都移到其自己的服务类中时,Action 的意图变得多么清晰。

接下来请注意,我们的类创建了两个动作,其中一个使用 json 结果类型。 json 结果将为我们将我们的列表序列化为 JSON。

这里使用了某种依赖注入魔法,但您也可以轻松地使用 prepare 方法和工厂。

此外,注释涵盖了所需的内容,不需要任何 xml,但是需要 struts2-conventions-plugin jar 和 struts2-json-plugin jar。

所以...您需要回溯并重建您的操作以获得您想要的功能,因为目前您有一个 json 字符串列表,这不是您想要写入 jsps 的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 2021-10-23
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多