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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

page numbering

Status
Not open for further replies.

sensory21

Programmer
Jan 27, 2004
79
GB
Dear all,

Below is my jsp page which print the pages numbering with previous and next when applicable; this code works fine but in order to improve the jsp page I thought it will look better if I was calling a java class or method? Any advice?

thanks
Vero



<%@ page import="java.sql.*, Mypackage.*" %>
<html xmlns=" lang="en-US" xml:lang="en-US">
<head>
<title></title>
</head>
<body>
<%
String url = application.getInitParameter("dbUrl");
String pwd = application.getInitParameter("dbPwd");
String user = application.getInitParameter("dbUser");
Connection conn = ConnectionManager.getConnection(url, user, pwd);
PreparedStatement prep = conn.prepareStatement("select count(*) as rowcount from customer");
ResultSet r = prep.executeQuery();
r.next();
int rowcount = r.getInt("rowcount") ;
r.close() ;

int records=5; //number of records displayed per page
int numberOfPages; //number of pages
int thispage; //current page number
int prev_page; //previous page
int next_page; //next page

if (rowcount <= records)
{
numberOfPages = 1;
}else{
if ((rowcount%records)==0)
{
numberOfPages=(rowcount/records);
}else{
numberOfPages=(rowcount/records)+1;
}
}

if((numberOfPages * records) >= rowcount)
{
numberOfPages++;
}

int currentRs;
String pt = request.getParameter("mv");
if (pt == null)
{
currentRs = 0;
thispage = 1;
prev_page = 0;

}else{
thispage = Integer.parseInt((String)pt);
currentRs = records * (thispage -1);
prev_page =thispage-1;
}

int cpage = numberOfPages-1;
out.println("&nbsp;");

if(prev_page >= 1)
{
out.println("<a href=test.jsp?mv=" + prev_page + " class=\"t\"><img border=\"0\" src=\"../images/back_arrow.gif\"> Previous</a>");
}

for(int p=1; p < numberOfPages; p++)
{
if(p == thispage)
{
out.println("<font color=\"#9999CC\" size=\"+2\">" + p + "</font>");
}else{
out.println("<a href=test.jsp?mv=" + p + ">" + p + "</a>");
}
}

if(cpage>=1)
{
next_page=thispage+1;
}else {
next_page=0;
}

if(thispage != cpage)
{
out.println("<a href=test.jsp?mv=" + next_page + " class=\"t\">Next <img border=\"0\" src=\"../images/more_arrow.gif\"></a>");
}//else{}

PreparedStatement prep1 = conn.prepareStatement("select company_name, contact_name, address, address2, city, pcode, country, phone, fax, email, website from customer order by company_name limit " + currentRs + "," + records);
ResultSet rs = prep1.executeQuery();
while (rs.next()) {

String fax = rs.getString("fax");
String email = rs.getString("email");
String website = rs.getString("website");
String charname = rs.getString("company_name");
%>
<table width="100%" border="0">
<tr class="display1">
<td><%= rs.getString("company_name") %>
</td>
</tr>
<%
if (rs.getString("contact_name").equals("")){} else { %>

<tr class="display3">
<td>Contact Name: <%= rs.getString("contact_name") %>
</td>
</tr>
<% } %>
<tr class="display3">
<td>Address: <%= rs.getString("address") %>,
<%
if (rs.getString("address2").equals("")){} else { %>

<%=rs.getString("address2") %>, <% } %>

<%= rs.getString("city") %>,<%= rs.getString("pcode") %> - <%= rs.getString("country") %>

</td>
</tr>
<tr class="display3">
<td>Telephone: <%= rs.getString("phone") %>
<td>
</tr>

<% if (fax.equals("")) {} else { %>

<tr class="display3">
<td>Fax: <%= rs.getString("fax") %>
</td>
</tr>
<% }

if (email.equals("")) {} else { %>

<tr class="display3">
<td>Email: <a href= "mailto:<%= rs.getString("email") %>"> <%= rs.getString("email") %></a>
</td>
</tr>
<% }

if (website.equals("")) {} else { %>

<tr class="display3">
<td>Website: <a href= " website %>" onClick="window.open(' website %>', 'newWin', 'toolbar=yes,location=yes,directories=no,status=no, menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width=800,height=460');return false;"> <%= website %></a>
</td>
</tr>
<% } %>
<tr>
<td align="right"><img src="../images/top_arrow.gif" border="no">&nbsp;<a href="#top" style="text-decoration:none"><font size="1">back to top</font></a>
</td>
</tr>
</table>
<% }//end while
conn.close();
%>
<!-- Repeat as required -->
<!-- End Page Layout -->
<!-- Trailing Footer -->
<hr width="100%"/>
<center class="copyright"><i>&copy; Copyright SensoryNet 2004</i></center>
</body>
</html>
 
Hi Siberian,

sorry didn't come back to you earlier as I've been busy doing other things but now Im back and trying to work out the display tag and have problems :-(

so I install the display tag library such as explained:

but not so sure about the configuration; the first time I've tried to run it, it said 'Nothing found to display' but now I get an Apache error:

Exception: [.LookupUtil] Error looking up property "BP" in object type "com.mysql.jdbc.ResultSet"
Cause: Unknown property 'BP'
org.displaytag.util.LookupUtil.getBeanProperty(LookupUtil.java:218)
org.displaytag.model.Column.getValue(Column.java:95)
org.displaytag.model.Column.createChoppedAndLinkedValue(Column.java:146)
org.displaytag.model.Column.getOpenTag(Column.java:122)
org.displaytag.tags.TableTag.getTableBody(TableTag.java:1481)
org.displaytag.tags.TableTag.getHTMLData(TableTag.java:1231)
org.displaytag.tags.TableTag.doEndTag(TableTag.java:925)
org.apache.jsp.jsp.test_jsp._jspService(test_jsp.java:250)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)


from this code:

PreparedStatement prep1 = conn.prepareStatement("select company_name, contact_name, address, address2, city, pcode, country, phone, fax, email, website from customer order by company_name limit " + currentRs + "," + records);
ResultSet rs = prep1.executeQuery();
rs.next();
request.setAttribute("results",rs);
%>
<display:table name="results">
<display:column property="<%= rs.getString("company_name") %>" />
<display:column property="<%= rs.getString("address") %>" />
<display:column property="<%= rs.getString("address2") %>" />
<display:column property="<%= rs.getString("city") %>" />
</display:table>


Error looking up property "BP" : BP is the first company name in my sql table

can you advise please?

Cheers
Vero
 
Sure thing. See, displaytag wants object properties but your giving an explicit value so its choking, badly, I don't even know if it can natively parse a ResultSet object.

The way I would handle this is read my resultset into a list of objects and then pass that list to Displaytag for formatting. You really should be encapsulating that data anyhow...

So, I would create a 'Company' object something like this :

Code:
import java.sql.*;
public class Company {
	private String companyname;
	private String address1;
	private String address2;
	private String city;
	
	
	public Company(ResultSet rs){
		try{
			setCompanyname(rs.getString("company_name"));
			setCity(rs.getString("city"));
			setAddress2(rs.getString("address2"));
			setAddress1(rs.getString("address1"));
		} catch(Exception e){
			// Somehow handle this
		}
	}
	/**
	 * @return
	 */
	public String getAddress1() {
		return address1;
	}

	/**
	 * @return
	 */
	public String getAddress2() {
		return address2;
	}

	/**
	 * @return
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @return
	 */
	public String getCompanyname() {
		return companyname;
	}

	/**
	 * @param string
	 */
	public void setAddress1(String string) {
		address1 = string;
	}

	/**
	 * @param string
	 */
	public void setAddress2(String string) {
		address2 = string;
	}

	/**
	 * @param string
	 */
	public void setCity(String string) {
		city = string;
	}

	/**
	 * @param string
	 */
	public void setCompanyname(String string) {
		companyname = string;
	}

}

Then, in your jsp you can do something like this :
Code:
PreparedStatement prep1 = conn.prepareStatement("select company_name, contact_name, address, address2, city, pcode, country, phone, fax, email, website from customer order by company_name limit " + currentRs + "," + records);
ResultSet rs = prep1.executeQuery();
List company_list = new List();
while(rs.next()){
    Company company = new Company(rs);
    company_list.add(company);
}

Now you can feed displaytag like this :

Code:
<display:table name="company_list">
  <display:column property="companyname" />
  <display:column property="address1" />
  <display:column property="address2"/>
  <display:column property="city"/>
</display:table>

Note : I DID NOT ACTUALLY TEST THIS. It probably has bugs and maybe even a logic error or two. Your core issue is that your trying to interact with stuff that uses the Getter/Setter paradigm directly and that won't work. You need to encapsulate your data into proper objects and work from there.
 
Hi Siberian,

thanks for your help but I cannot make it work! Have tried your code, made modifications but still get errors...

//one of them is:
java.util.List is abstract; cannot be instantiated
//for this piece of code
List customers_list = new List();

should I use ArrayList()?

and another error:
setName(java.lang.String) in org.displaytag.tags.TableTag cannot be applied to (java.util.List)

any ideas?

Many thanks
Vero

 
Dunno, I'm using the 1.4.2 JDK if thats any help. It looks like a single cascading error based on the lack of a List function. Switch it out and see what happens. Read the API docs and learn how ot construct a List. Ultimately, displaytag needs a list of objects that have properties.
 
Didn't you say that you have been using the Displaytag, in what circumstances? how? can you give an example?

I wish I could make it work; I like the layout of the tags and could be an awful lot less code in the pages for all the tables!
 
I used it briefly on a project but things got a bit more complex then it could easily handle.

In its basic usage though its really great. Essentially here is what you need to do

1) Create an object that represents your 'row' entry. This object should conform to the standard getter/setter methodology. If you don't have this you'll never make it work easily.

2) Create a list of these objects.

3) Feed the list to displaytag.

4) In each table cell you just tell displaytag which property (variable) to display, it will handle the getter transformation and retrieve it.

Its pretty simple if your knowledgable of the getter/setter paradigm and you are working with objects rather then inline jsp code.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top