How to conver ${row.count} to Java variable?

<sql:query var="count" dataSource="jdbc/Trade18">

SELECT count(*) AS count FROM company where status='wait' and diylevel='1' and (domainname='' OR domainname is null) order by regdate DESC

</sql:query>

<c:forEach var="row" items="${count.rows}">

<c:if test="${row.count > 0}">

<%

Integer pagesize = 20;

Integer pages = (int)((${row.count}-1/pagesize)+1); //Question line

out.println(pages);

%>

</c:forEach>

How can I convert this ${row.count} variable to JAVA variable? Thx.-_-

[595 byte] By [roamera] at [2007-11-26 16:30:17]
# 1

What is the type of the variable 'row'? ResultSet ?

Then it would be

Integer pages = ((ResultSet)pageContext.findAttribute("rows")).getCount();

The more appropriate way would be to use jstl arithmetic operators itself in place of the scriptlets

You need to delare the pageSize variable once.

<c:set var = "pageSize" value = "20"/>

And later your entire scriptlet code within the loop would be

<c:out value = "${ (row.count - 1/pageSize) + 1}"/>

Does that help?

ram.

Madathil_Prasada at 2007-7-8 22:54:43 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
An even more elegant solution would be to use the begin and end attributes of the forEach tag to specify which entries to print out.The purpose of JSTL is to eliminate scriptlet code. Mixing the two makes a page even more unreadable than scriptlet code alone.
evnafetsa at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Actually var="count" is the ResultSet, where var="row" is each record, row.count is the field value for that particular record.I will take a try, thx for all.
roamera at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
<c:out value = "${ (row.count - 1/pageSize) + 1}"/>The output is an floating point value, such as 1899.55, how can i convert it to integer, 1899?
roamera at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> <c:out value = "${ (row.count - 1/pageSize) + 1}"/>

>

> The output is an floating point value, such as

> 1899.55, how can i convert it to integer, 1899?

Use the jstl fmt tag library.

<fmt:formatNumber value = "${ (row.count - 1/pageSize) + 1}" pattern = "0"/>

ram.

Madathil_Prasada at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Thx for all guys.

Actually, I want to convert existing PHP codes to JSP:

$mysqlstr="SELECT * FROM table";

$rows=mysql_query($mysqlstr);

$pagesize=20;

$pages=intval(($totalrecord-1)/$pagesize)+1;

if($page<1) $page=1;

if($page>$pages) $page=$pages;

$position=($page-1)*$pagesize;

mysql_data_seek($rows,$position);

--

I am a learner in JSP so I am not familiar what type of utilities should be used to achieve this. JSTL? How to let the var useable in Java? -_-

roamera at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Ok, looking at this, it seems to be doing "paging" - ie displaying results 20 at a time, with a page x of y interface.

It seems to be using variables which are set elsewhere.

In particular $page which I presume indicates which page x you are currently viewing.

There are a couple of approaches to use here

1 - Use the [url http://jsptags.com/tags/navigation/pager/pager-taglib-2.0.html] Pager taglib[/url]

2 - Use JSTL

<c:forEach var="row" items="${queryResults}" begin="${position}" end="${position + pagesize} >

...

</c:forEach>

Of course you will have to set up the variables yourself.

Hope this helps some,

evnafets">

evnafetsa at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thx much.It seems not easy to acheive this, I have to study more general concept before acheive this effect.
roamera at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
<c:set var="count" value="${row.count}" scope="request"></c:set><% Integer cou = (Integer) request.getAttribute("count");%>
golanblna at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
I got other problem,java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.IntegerThe value is actually 1899, why it is regarded as Long? String can't cast back to Integer....
roamera at 2007-7-8 22:54:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...