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

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.)

[QUOTE]

<%@ include file="../common/page_imports.jsp" %>

<inquiry:tableClaimHistory numberOfRecords=<%=ic.getTotalNumOfRecords()%> dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

[/QUOTE]

Error Message:

claimHistoryView.jsp:190:3: Unterminated tag.

<inquiry:tableClaimHistory numberOfRecords=<%=ic.getTotalNumOfRecords()%> dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

2.)

[QUOTE]

<%@ include file="../common/page_imports.jsp" %>

<inquiry:tableClaimHistory numberOfRecords="<%=ic.getTotalNumOfRecords()%>" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

[/QUOTE]

Error Message:

claimHistoryView.jsp:190:4: The required attribute "numberOfRecords" is missing.

<inquiry:tableClaimHistory numberOfRecords="<%=ic.getTotalNumOfRecords()%>" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

3.)

[QUOTE]

<%@ include file="../common/page_imports.jsp" %>

<inquiry:tableClaimHistory numberOfRecords="<%ic.getTotalNumOfRecords();%>" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

[/QUOTE]

Error Message:

java.lang.NumberFormatException: For input string: "<%ic.getTotalNumOfRecords();%>"

4.)

[QUOTE]

<%@ 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"/>

[/QUOTE]

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.)

[QUOTE]

<%

int records1 = ic.getTotalNumOfRecords();

Integer records2 = new Integer(records1);

String numberOfRecords2 = records2.toString();

%>

<inquiry:tableClaimHistory numberOfRecords=<%numberOfRecords2;%> dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

[/QUOTE]

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:

[quote]

InquiryContext ic = InquiryContext.getContext(session);

[quote]

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?

[quote]

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=\"" + "http://www.google.ca"+ "\"" + " 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);

}

}

[quote]

Any hint would be greated appreciated.

Yours,

John Smith

[7048 byte] By [jadeite100a] at [2007-11-27 7:14:00]
# 1

Ok, couple of things

1 - ALWAYS put quotes around attributes in a custom tag. That rules out items #1 and #5 as incorrect.

2 - You were correct using the <%= expr %> tags. <% scriptlet %> tags are not used as attributes to custom tags. That rules out #3 and #4

#2 looks the closest:

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"/>

Check your spelling of that attribute. It looks right here,.

You also said that ic.getTotalNumOfRecords returns an int, while the attribute returns a String

Try

<%@ include file="../common/page_imports.jsp" %>

<inquiry:tableClaimHistory numberOfRecords="<%="" + ic.getTotalNumOfRecords()%>" dataAction="claimHistoryViewData.do" emptyKey="error.noData"/>

<%= "" + ic.getTotalNumOfRecords %> is the cop-out way to convert an int to a String :-)

evnafetsa at 2007-7-12 19:04:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi:

I appreciated your effort.

I tried your suggestions above.

I get the following error:

This attribute is not recognized.

I couldn't pass the value through the tag but I figured a way to do it through the java code that inherited the tag class.

It looks like maybe there is no way to do it through an attribute of a custom tag.

Thank you for effort.

Greatly appreciated!!

Thanks.

jadeite100a at 2007-7-12 19:04:31 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...