【发布时间】: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(如果有)和操作。精简版的动作会很棒,但可能没有必要。
-
我已经添加了代码,请检查它