1、从《java整合web开发王者归来》上到的使用jstl进行前台页面分页,想重现一遍,就把代码copy了一 下,但是在本地使用的时候出现了如下问题,百度了下感觉是JSTL标签库的问题,把网上给出的办法试了一遍,基本没什么作用,过来求教。
2、附上jsp的源码:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="com.Pagination"%>
<html>
<%
request.setAttribute("pagination",new Pagination(request,response));
%>
<!-- 设置数据源 -->
<sql:setDataSource driver="com.mysql.jdbc.driver" user="root" password="root"
url="jdbc:mysql://localhost:3306/mysql?charachterEncoding=UTF-8" var="dataSource"/>
<sql:query var="rs" dataSource="${dataSource}">
select count(*) count from help_topic
</sql:query>
<c:forEach var="row" items="${rs.rows}">
<jsp:setProperty name="pagination" property="recordCount" value="${row.count}"/>
</c:forEach>
<sql:query var="rs" dataSource="${dataSource}" startRow="${pagination.firstResult}"
maxRows="${pagination.pageSize}">
select * from help_topic
</sql:query>
<head>
<title>Title</title>
</head>
<body>
<table>
<tr class="title">
<td>Help_ID</td>
<td>Name</td>
<td>Description</td>
</tr>
<c:forEach var="row" items="${rs.rows}">
<tr>
<td align="center">${row['help_topic_id']}</td>
<td>${row['name']}</td>
<td>${row['description']}</td>
</tr>
</c:forEach>
</table>
</br>
</body>
${pagination}
</html>
3、Pagination.java源码:
package com;/*
* IBM Confidential
*
* OCO Source Materials
*
* #ID# IBM CRL Supply Chain Management Research
*
* (C) Copyright IBM Corp. 2005, 2006
*
* The source code for this program is not published or otherwise divested of
* its trade secrets.
*
*/
import java.net.URLEncoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Pagination
{
private int pageSize = 20;
private int pageNum = 1;
private int recordCount;
private int pageCount;
private int firstResult;
private String pageUrl;
public Pagination(HttpServletRequest request, HttpServletResponse response)
{
try
{
pageNum = Integer.parseInt(request.getParameter("pageNum"));
}
catch (Exception e)
{
}
for (Cookie cookie : request.getCookies())
{
if ("pageSize".equals(cookie.getName()))
{
try
{
pageSize = Integer.parseInt(cookie.getValue());
}
catch (Exception e)
{
}
}
}
try
{
pageSize = Integer.parseInt(request.getParameter("pageSize"));
}
catch (Exception e)
{
}
Cookie cookie = new Cookie("pageSize", Integer.toString(pageSize));
cookie.setMaxAge(Integer.MAX_VALUE);
response.addCookie(cookie);
StringBuffer queryString = new StringBuffer();
for (Object parameterName : request.getParameterMap().keySet())
{
String name = (String) parameterName;
if ("pageNum".equals(name) || "pageSize".equals(name))
{
continue;
}
for (String value : request.getParameterValues(name))
{
if (queryString.length() > 0)
{
queryString.append("&");
}
try
{
queryString.append(name + "="
+ URLEncoder.encode(value, "UTF-8"));
}
catch (Exception e)
{
queryString.append(name + "=" + value);
}
}
}
pageUrl = request.getRequestURI() + "?" + queryString.toString();
}
private void calculate()
{
pageCount = (recordCount + pageSize - 1) / pageSize;
firstResult = (pageNum - 1) * pageSize;
}
/**
* 生成分页信息 包括第一页,上一页,下一页,最后一页等等。
*
* @param pageNum
* 当前页数
* @param pageCount
* 总页数
* @param recordCount
* 总记录数
* @param pageUrl
* 页面 URL
* @return
*/
public String toString()
{
calculate();
String url = pageUrl.contains("?") ? pageUrl : pageUrl + "?";
StringBuffer buffer = new StringBuffer();
buffer.append("每页 ");
buffer
.append("<select name=ibm_crl_scm_page_size_select onchange='setPageSize(value); ' >");
buffer.append(" <option value=5"
+ (pageSize == 5 ? " selected " : "") + ">5</option>");
buffer.append(" <option value=20"
+ (pageSize == 20 ? " selected " : "") + ">20</option>");
buffer.append(" <option value=40"
+ (pageSize == 40 ? " selected " : "") + ">40</option>");
buffer.append(" <option value=60"
+ (pageSize == 60 ? " selected " : "") + ">60</option>");
buffer.append(" <option value=80"
+ (pageSize == 80 ? " selected " : "") + ">80</option>");
buffer.append(" <option value=100"
+ (pageSize == 100 ? " selected " : "") + ">100</option>");
buffer.append("</select> 条记录 ");
buffer.append(" 总记录数: " + recordCount);
buffer.append(" 页数/总页数: " + pageNum + "/" + pageCount + " ");
buffer.append(" ");
buffer.append(pageCount == 0 || pageNum == 1 ? " 第一页 " : " <a href='"
+ url + "&pageNum=1'>第一页</a> ");
buffer.append(" ");
buffer.append(pageCount == 0 || pageNum == 1 ? " 上一页 " : " <a href='"
+ url + "&pageNum=" + (pageNum - 1) + "'>上一页</a> ");
buffer.append(" ");
buffer.append(pageCount == 0 || pageNum == pageCount ? " 下一页 "
: " <a href='" + url + "&pageNum=" + (pageNum + 1)
+ "'>下一页</a> ");
buffer.append(" ");
buffer.append(pageCount == 0 || pageNum == pageCount ? " 最后一页 "
: " <a href='" + url + "&pageNum=" + pageCount + "'>最后一页</a> ");
buffer
.append(" 转到第<input type='text' name='ibm_crl_scm_goto_input' "
+ " style='width:20px; font-size:12px; text-align:center; '>页 ");
buffer.append(" <input type='button' "
+ " name='ibm_crl_scm_goto_button' value='Go' class='button'>");
buffer.append("<script language='javascript'>");
buffer.append("function helloweenvsfei_enter(){");
buffer.append(" if(event.keyCode == 13){");
buffer.append(" helloweenvsfei_goto();");
buffer.append(" return false;");
buffer.append(" }");
buffer.append(" return true;");
buffer.append("} ");
buffer.append("function setPageSize(pageSize){");
buffer.append(" location='" + url + "&pageSize=' + pageSize;");
buffer.append("} ");
buffer.append("function helloweenvsfei_goto(){");
buffer
.append(" var numText = document.getElementsByName('ibm_crl_scm_goto_input')[0].value;");
buffer.append(" var num = parseInt(numText, 10);");
buffer.append(" if(!num){");
buffer.append(" alert('Input must be a number'); ");
buffer.append(" return;");
buffer.append(" }");
buffer.append(" if(num<1 || num>" + pageCount + "){");
buffer.append(" alert('Input must between 1 and " + pageCount
+ ". '); ");
buffer.append(" return;");
buffer.append(" }");
buffer.append(" location='" + url + "&pageNum=' + num;");
buffer.append("}");
buffer
.append("document.getElementsByName('ibm_crl_scm_goto_input')[0].onkeypress = helloweenvsfei_enter;");
buffer
.append("document.getElementsByName('ibm_crl_scm_goto_button')[0].onclick = helloweenvsfei_goto;");
buffer.append("</script>");
return buffer.toString();
}
public int getPageSize()
{
calculate();
return pageSize;
}
public void setPageSize(int pageSize)
{
calculate();
this.pageSize = pageSize;
}
public int getRecordCount()
{
calculate();
return recordCount;
}
public void setRecordCount(int recordCount)
{
calculate();
this.recordCount = recordCount;
}
public int getFirstResult()
{
calculate();
return firstResult;
}
public void setFirstResult(int firstResult)
{
calculate();
this.firstResult = firstResult;
}
public String getPageUrl()
{
return pageUrl + "&pageNum=" + pageNum;
}
public void setPageUrl(String pageUrl)
{
this.pageUrl = pageUrl;
}
}
// end
4、错误信息:
十二月 21, 2016 6:23:57 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [jsp] in context with path [] threw exception [The absolute uri: http://java.sun.com/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application] with root cause
org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
at org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:56)
at org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:445)
at org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:117)
at org.apache.jasper.compiler.TagLibraryInfoImpl.generateTLDLocation(TagLibraryInfoImpl.java:325)
at org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:154)
at org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:419)
at org.apache.jasper.compiler.Parser.parseDirective(Parser.java:484)
at org.apache.jasper.compiler.Parser.parseElements(Parser.java:1421)
at org.apache.jasper.compiler.Parser.parse(Parser.java:138)
at org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242)
at org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:958)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1087)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
*********************************************************
补充:
jar包是导入了的,我这里的版本是jstl-1.2,这个报错信息我从网上查的都是说什么jstl库版本问题的,但是我试着导入jstl-1.1.jar依然不行。
你的web.xml中配置jstl了吗