Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass a server side value to an attribute of a custom jsp tag

Status
Not open for further replies.

jadeite100

Programmer
May 17, 2006
19
CA
Hi All:

I needed to passed an integer value from the following code:
<%=ic.getTotalNumOfRecords()%>
to an attribute of a custom tag
<inquiry:tableClaimHistory numberOfRecords="5" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

The function getTotalNumOfRecords returns an int.
The attribute numberOfRecords expects an string.

Here are the different ways I tried in a jsp page but I get also the following errors:
1.)
<%@ include file="../common/page_imports.jsp" %>
<inquiry:tableClaimHistory numberOfRecords=<%=ic.getTotalNumOfRecords()%> dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

Error Message:
claimHistoryView.jsp:190:3: Unterminated tag.
<inquiry:tableClaimHistory numberOfRecords=<%=ic.getTotalNumOfRecords()%> dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

2.)
<%@ include file="../common/page_imports.jsp" %>
<inquiry:tableClaimHistory numberOfRecords="<%=ic.getTotalNumOfRecords()%>" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>
Error Message:
claimHistoryView.jsp:190:4: The required attribute "numberOfRecords" is missing.
<inquiry:tableClaimHistory numberOfRecords="<%=ic.getTotalNumOfRecords()%>" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

3.)
<%@ include file="../common/page_imports.jsp" %>
<inquiry:tableClaimHistory numberOfRecords="<%ic.getTotalNumOfRecords();%>" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>
Error Message:
java.lang.NumberFormatException: For input string: "<%ic.getTotalNumOfRecords();%>"


4.)
<%@ include file="../common/page_imports.jsp" %>
<%
int records1 = ic.getTotalNumOfRecords();
Integer records2 = new Integer(records1);
String numberOfRecords2 = records2.toString();
%>
<inquiry:tableClaimHistory numberOfRecords="<%numberOfRecords2;%>" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>
error message:
java.lang.NumberFormatException: For input string: "<%numberOfRecords2;%>"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.valueOf(Double.java:447)
at java.lang.Double.(Double.java:539)
at com.DisplayTableClaimHistoryTag.displayTable(DisplayTableClaimHistoryTag.java:63)


5.)
<%
int records1 = ic.getTotalNumOfRecords();
Integer records2 = new Integer(records1);
String numberOfRecords2 = records2.toString();
%>
<inquiry:tableClaimHistory numberOfRecords=<%numberOfRecords2;%> dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>
error message:
claimHistoryView.jsp:194:3: Unterminated tag.
<inquiry:tableClaimHistory numberOfRecords=<%numberOfRecords2;%> dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

In the custom tag java code called "DisplayTableClaimHistoryTag"

I tried to used the following code:
InquiryContext ic = InquiryContext.getContext(session);

The problem is that in order to get session I needed HttpSession object. I don't know how to passed HttpSession "session" object
to a custom tag. Is there a way to do this?
public class DisplayTableClaimHistoryTag extends InquiryTag
{
String numberOfRecords;
public void setNumberOfRecords(String numberOfRecords)
{
this.numberOfRecords = numberOfRecords;
}

public String getNumberOfRecords()
{
return numberOfRecords;
}

public int doStartTag()throws JspException
{

InquiryContext context = (InquiryContext)pageContext.getSession().getAttribute(Constrain.CONTEXT);
if(context==null)
throw new JspException(TAG_EXCEPTION+ "InquriyContext is null.");

String hasData = (String)context.getAttribute(Constrain.CONTROL_HAS_DATA);
if(hasData==null)
throw new JspException(TAG_EXCEPTION + "The hasData property can not be null.");

boolean hd = Boolean.valueOf(hasData).booleanValue();
Debug.println("hasData="+hd);
Debug.println("hasDataString="+hasData);

if(hd)
displayTable();
else
disPlayError();

return SKIP_BODY;
}

private void displayTable() throws JspException
{

String outString ="";
Debug.println("dispalyTable() ********* dataAction="+ dataAction);
JspWriter out = pageContext.getOut();
/*
* Minimum height height= 103,70
* 21.7 per row
* First row==103+21.5=124.5
* Second row ==103+21.5*2=146
* Third row ==103+21.5*3=167.5
*/
Double numberOfRecordsBigDouble = new Double(numberOfRecords);
double numberOfRecordsDouble = 70 + 21.8*numberOfRecordsBigDouble.intValue();
if(order==null || order.equals("0"))
// outString = "<iframe src=\"" + "/inquiry/" + dataAction + "?order=0"+ "\"" + " name=\"dataFrame\" id=\"dataFrame\" height=\""+numberOfRecordsDouble+"\"" +" width=\"100%\" scrolling=\"NO\" frameborder=\"0\"></iframe>";
// outString = "<iframe src=\"" + "/inquiry/" + dataAction + "?order=0"+ "\"" + " name=\"dataFrame\" id=\"dataFrame\" style=\"height:"+numberOfRecordsDouble+"px; width:100%\" scrolling=\"NO\" frameborder=\"0\"></iframe>";
// outString = "<iframe src=\"" + " "\"" + " name=\"dataFrame\" id=\"dataFrame\" style=\"height:"+numberOfRecordsDouble+"px; width:100%\" scrolling=\"NO\" frameborder=\"0\"></iframe>";
outString = "<iframe src=\"" + "/inquiry/" + dataAction + "?order=0"+ "\"" + " name=\"dataFrame\" id=\"dataFrame\" style=\"height:"+numberOfRecordsDouble+"px; width:100%\" scrolling=\"NO\" frameborder=\"0\"></iframe>";

else
{
String orderStr = "?order=" + order;
outString = "<iframe src=\"" + "/inquiry/" + dataAction + orderStr + "\"" + " name=\"dataFrame\" id=\"dataFrame\" height=\""+numberOfRecordsDouble+"\"" +" width=\"100%\" scrolling=\"NO\" frameborder=\"0\"></iframe>";
//outString = "<iframe src=\"" + "/inquiry/" + dataAction + orderStr + "\"" + " name=\"dataFrame\" id=\"dataFrame\" height=\"161\" width=\"100%\" scrolling=\"NO\" frameborder=\"0\"></iframe>";
}
Debug.println("dispalyTable() ********* outString = "+ outString);

try {
out.println(outString);
} catch (IOException e) {
this.log.error(TAG_EXCEPTION + e.toString(), e);
throw new JspException(e);
}
}

Any hint would be greated appreciated.

Yours,

Frustrated
 
Have you tried
Code:
<inquiry:tableClaimHistory numberOfRecords="${ic.totalNumOfRecords}" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

.. though my JSP is a little weak and I've not tried this myself.

Tim
 
Hi:

Thank you for your suggestions.
I tried your suggestions and I get the following error:
java.lang.NumberFormatException: For input string: "${ic.totalNumOfRecords}"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
at java.lang.Double.valueOf(Double.java:447)
at java.lang.Double.(Double.java:539)


I even tried the following code:
<inquiry:tableClaimHistory numberOfRecords="${"" + ic.totalNumOfRecords}" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

I get the following error message:
claimHistoryView.jsp:190:54: This attribute is not recognized.
<inquiry:tableClaimHistory numberOfRecords="${"" + ic.totalNumOfRecords}" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

Thank You for your help!!!
 
Mmmm. Looking at the error, the tag library looks to be using the valueOf method of Double to parse a number in textual form into a native double value. To my mind, this means you have got no choice but to put in a literal number value since this is the only form which will parse to a numeric double.

Again, not my main area of Java expertise, but since it's a custom tag, there may be an accepted way for the tag implementation to process it's arguments so they go through the JSP environment processing and thus allow you to pick up values in the way you want.

Tim
 
Before I spent time reading up on writing custom jsp tags, is the code implementing your custom tag available to you to change?

Tim
 
... in the first page of 'Custom Tags in JSP Pages' in the J2EE Tutorial from Sun ...
Simple tag handlers can be used only for tags that
do not use scripting elements in attribute values or the tag body. Classic tag handlers
must be used if scripting elements are required.

You want scripting elements within attribute values so if the custom tag is implemented as a 'simple' tag handler it won't work like you need it to.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top