2012-03-06 25 views
1

我回來了一件更復雜的事情。這一次我不知道我是否在邏輯上正確。如何在struts中將json值作爲數據庫對象返回hibernate

我必須從數據庫返回數據作爲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有一個數據庫對象列表,我可以通過訪問該這樣

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

得到productRating表的值,該代碼我有表這裏是第一頁,如果用戶點擊第二頁,我必須列出用戶的第二頁,我們需要通過填充json值來在ajax中執行此操作。我的問題是我怎麼能這樣寫一個迭代器,如果我檢索答案作爲Ajax?

這裏是我的Action類

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在這裏。這裏是我的道功能從那裏我從數據庫

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調用。

+1

顯示一些代碼,如果你已經嘗試任何事情究竟是你碰到的問題。 – Quaternion 2012-03-06 16:45:29

+0

您是否使用註釋或XML來指定動作?如何顯示xml(如果有)和操作。該行動的修剪版本會很好,但可能不必要。 – Quaternion 2012-03-06 21:38:09

+1

我已經加入了代碼請你只檢查 – user359187 2012-03-07 06:22:49

回答

0

這將是對我的部分猜測...

簡短的回答你的問題是:沒有。那就是既不能返回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; 
    } 
} 

上述的說明:

通過讓我們的行動落實製備的struts2的將調用準備它使用doctorService設置doctorProducts方法。請注意,當與醫生管理有關的所有事情都已轉移到其自己的服務類別(包括事務管理)時,該操作的意圖如何變得清晰。

接下來說明我們的類創建了兩個動作,其中一個使用了json結果類型。 json結果會將我們的列表序列化爲JSON。

這裏使用了某種依賴注入魔法,但是您可以像使用prepare方法和工廠一樣容易。

此外,註解涵蓋了什麼是需要的,不需要任何xml,但是需要使用struts2-conventions-plugin jar和struts2-json-plugin jar。

所以......你需要回溯和重建行動,以獲得您想要的功能,因爲現在你有JSON字符串列表這是不是你想寫到你的JSP中的內容。

+0

謝謝,我會檢查這個。 – user359187 2012-03-15 07:28:25

相關問題