How to call nested methods from Javabean to JSP
Hi all
I have an array as follows.
Tag tagList[] = reader.getTagList();
From above array I am calling three nested methods(tag.getTagID(), tag.getAntenna(), and tag.getRenewCount) as
for (int i=0; i<tagList.length; i++){
Tag tag = tagList[i];
System.out.println("Tag ID:" + tag.getTagID() +
", Antenna:" + tag.getAntenna() +
", Read Counts:" + tag.getRenewCount()
);
}
This code works well in core java. But I am not sure how get and set these nested methods in javabean to call in JSP.
Could anyone help me how to convert this code in javabean then I can call from JSP?
Thanx
kvijai
Message was edited by:
kvijai>
[926 byte] By [
kvijaia] at [2007-11-26 18:39:16]

# 1
Use logic:iterate to iterate over the array in some page,request,session or application context and write the property content using bean:write tag as shown below.
<logic:iterate name="tagList" id="tag">
<bean:write name="tag" property="id"/>
<bean:write name="tag" property="antenna"/>
<bean:write name="tag" property="renewCount"/>
</login:iterate>
# 2
Thanx Vishwas_Prasanna The code I have given is from core java class, so now how do I manage these three methods in my javabean? Do I need another javabean class to map it?. I never used logic:iterate. Thanxkvijai
# 3
I dont know what component you are using whther struts action servlet, serlvet or using jsp directly.
Based on your enviroment you can use struts tag library, JSTL or scriplets.
And the logic iterate works like this.
It iterates over the tagList and binds the value in that iteration to the name specified in the id attribute which in the above case will hold a Tag type object. Once you have Tag object you can call methods on that. Don't append get or set the tag will do it for you.
# 4
hiBesically I am using only JSP and calling methods from Javabean. So which one is best for my case struts tag library, JSTL or scriplets.kvijai
# 5
Using srciplets is considered as bad since it will be difficult to author by non-java programmer like HTML,CSS guy. So go for JSTL. http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html
# 6
Sorry I am confused about these technologies.
I am using Tomcat -5.5.
Could you please provide me some example how to use scriplet or JSTL.
Is this <logic:iterate> part of JSP or do i need to install something?
Suppose if i use JSTL, do i need to install this or its already in TOMCAT-5.5.
Thanx
kvijai
# 7
JSTL tutorial from sun http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSTL3.html
# 8
Hi
I have gone through JSTL but did not get how to call these methods from JAVABEAN to jsp. They all talk only using pure JSP and no use of JavaBean. I think this <logic:iterate> is NOT part of JSTL.
Now let me concentrate on your first post using <logic:iterate>. Using <logic:iterate> I am giving my JSP page as follows.
<%@ page language="java" %>
<%@ page import="java.util.*"%>
<jsp:useBean id="readerALR9800" class="RFIDReaders.ALR9800.ALR9800Reader" scope="session"/>
<jsp:setProperty name="readerALR9800" property="*"/>
<jsp:include page="sendALR9800.jsp" />
<%
//Calling method ReaderData()
readerALR9800.ReaderData();
// Getting tag data from reader
<logic:iterate name="tagList" id="tag">
<bean:write name="tag" property="tagID"/>
<bean:write name="tag" property="antenna"/>
<bean:write name="tag" property="renewCount"/>
</logic:iterate>
%>
But If i use above page I can see several errors.
Please let me know where I am wrong and how to achieve this.
Thanx
kvijai
# 9
> I think this <logic:iterate> is NOT part of JSTL.
Your are Absolutely correct about it and it seems like JSTL is also not configured.
Basically the value which you provide for the name attribute should be in one of the scope page,request,session or applicaiton.
what you can do to your JSP temporarily to check out the data is as follows.
<%@ page import="Tag class"/>
<%
Tag tagList[] = readerALR9800.ReaderData();
// I am assuming that readerALR9800.ReaderData() and reader.getTagList() are same.
for (int i=0; i<tagList.length; i++) {
Tag tag = tagList[i];
%>
Tag ID: <%= tag.getTagID() %>
Antenna: <%= tag.getAntenna() %>
Read Counts: <%= tag.getRenewCount() %>
<%
}
%>
# 10
Thank you very much Vishwas
Now I have done it but I need to do one more thing
I need to add this <%= tag.getRenewCount() %> and then calculate the ratio of <%= tag.getRenewCount() %> and readTry. I am providing you my following JSP which is running successfully with values of <%= tag.getRenewCount() %> and readTry.
<%@ page language="java" %>
<%@ page import="java.util.*"%>
<%@ page import="com.alien.enterpriseRFID.reader.*,com.alien.enterpriseRFID.tags.*" %>
<jsp:useBean id="readerALR9800" class="RFIDReaders.ALR9800.ALR9800Reader" scope="session"/>
<jsp:setProperty name="readerALR9800" property="*"/>
<jsp:include page="sendALR9800.jsp" />
<html>
<head>
</head>
<body>
<hr><table border=1 cellspacing=2 cellpadding=1>
<tr>
<th>S No</th>
<th>TAG ID</th>
<th>READ COUNT</th>
<th>ANTENNA</th>
</tr>
<%
readerALR9800.ReaderData();
Tag tagList[] = readerALR9800.getTagList();
int readTry =readerALR9800.getReadTries();
out.println(readTry);
for (int i=0; i<tagList.length; i++) {
Tag tag = tagList[i];
%>
<tr><td><%=(i+1) %> </td>
<td><%= tag.getTagID() %></td>
<td><%= tag.getRenewCount() %></td>
<td><%= tag.getAntenna() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
Thanx
kvijai
# 11
<%
readerALR9800.ReaderData();
Tag tagList[] = readerALR9800.getTagList();
int readTry =readerALR9800.getReadTries();
out.println(readTry);
int sumOfRenewCount = 0;
for (int i=0; i<tagList.length; i++) {
Tag tag = tagList[i];
sumOfRenewCount = sumOfRenewCount + tag.getRenewCount();
// Assuming getRenewCount() is returning int value.
%>
<tr><td><%=(i+1) %> </td>
<td><%= tag.getTagID() %></td>
<td><%= tag.getRenewCount() %></td>
<td><%= tag.getAntenna() %></td>
</tr>
<%
}
int ratio = sumOfRenewCount/ readTry ;
%>
Ration is : <%= ration%>
Please undertand the difference between <% %>,<%= %>,<%! %> this will help you to integrate Java code easily in JSPs.
If my suggestions are helping you then reward me some DUKES.