我回來了一件更復雜的事情。這一次我不知道我是否在邏輯上正確。如何在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調用。
顯示一些代碼,如果你已經嘗試任何事情究竟是你碰到的問題。 – Quaternion 2012-03-06 16:45:29
您是否使用註釋或XML來指定動作?如何顯示xml(如果有)和操作。該行動的修剪版本會很好,但可能不必要。 – Quaternion 2012-03-06 21:38:09
我已經加入了代碼請你只檢查 – user359187 2012-03-07 06:22:49