I am back with one more complex thing. This time i don’t know whether i am logically correct.
I have to return the data from database as json object. In this json object i have return some values which is foreign key with other tables in the database. I got the values correctly from database and now i want to iterate the forien key values from my ajax success using the struts iterate tag. It is possible to iterate if we can iterate the json values an object, Anybody please share the idea if i am correct.
<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>
In this jsp page you can see that i have a database object list in listdoctorProducts and i can get the values of productRating table by accessing this like this
<s:iterator value="inventory.productRatings" status="st">
<s:if test="(#st.index+1)==1">
<s:set var="no2" value="rating" />
</s:if>
and this code i have list here is for first page and if user click for second page i have to list the second page for the user and we need to do this in ajax by populating json values. My problem is how could i write an iterator like this if i retrieve the response as ajax?
Here is my Action class
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;
}
I have not added the variable declaration and setter and getter here. And here is my Dao function from where i access the data from database
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;
}
and my struts.xml is
<action name="frPdtListPagination" class="com.zoondia.action.DoctorPdtsPagination" method="getEcommerceWidgetFourJson">
<result type="json">
</result>
</action>
Just add the action for this json call only.
This is going to be guess work on my part…
The short answer to your question is: no. That is you can’t both return json and iterate over the objects in your action producing meaningful results because of the way it has been set up.
Lets change the set up so the answer becomes a yes. This is the quick version not tested:
Explanation of the above:
By having our Action implement Preparable struts2 will call the prepare method which uses the doctorService set doctorProducts. Note how clean the intention of the Action becomes when everything to do with Doctor management has been moved into its own service class, including transaction management.
Next note that our class creates two actions, one of which uses the json result type. The json result will serialise our list into JSON for us.
Some sort of dependency injection magic was used here, but you could have just as easily used the prepare method and a factory.
Further, the annotations cover what is required there is no need for any xml, however there is need for struts2-conventions-plugin jar and struts2-json-plugin jar.
So… you’ll need to backtrack and rebuild your action to get the functionality you want because currently you have a list of json strings which is not what you want to write into your jsps.