当前位置:编程学习 > JAVA >>

jsp利用标签却无法显示,求大手帮忙~~~~

jsp页面

<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ taglib prefix="mytag" uri="http://coolszy.com/tagslib" %>
<td align="left" valign="top">
              <div class="title">家具用品</div>
              <mytag:goodsType type="家具用品" name="goods"></mytag:goodsType>
              <mytag:GoodsInfo url="jjgoods.jsp" type="家具用品" name="goods">    </mytag:GoodsInfo>
</td>

mytags.tld标签库

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.2</jspversion>
<shortname></shortname>
<uri>http://coolszy.com/tagslib</uri>
<info></info>
<tag>
<name>goodsType</name>
<tagclass>taglib.GoodsTypeTag</tagclass>
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>

<tag>
<name>GoodsInfo</name>
<tagclass>taglib.GoodsInfoTag</tagclass>
<attribute>
<name>name</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>type</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>url</name>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

类的实现
GoodsTypeTag

package taglib;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import entity.Goods;
import services.GoodsServices;

public class GoodsTypeTag extends TagSupport{
private int currentPage;//记录当前页面
private final int recordsInOnePage = 6;//设置每页显示数目

private String name;
private String type;  //商品类型
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

@Override
public int doStartTag() throws JspException {
//获取客户端请求信息中的pageNumber属性,它表示当前页码
//0表示第一页,1表示第二页,以此类推
String pageNumber=pageContext.getRequest().getParameter("pageNumber");
//如果pageNumber为空,表示第一页
if(pageNumber==null)
pageNumber="0";
//获取页码
currentPage=Integer.parseInt(pageNumber);
//System.err.println(currentPage);
return SKIP_BODY;
}


public int doEndTag() throws JspException {
try {
GoodsServices goodsServices=new GoodsServices();
//根据商品类型,每页显示数目和当前页面数获取符合条件的结果集。
List<Goods> result=goodsServices.getGoodsList(type, recordsInOnePage, currentPage);
//保存记录
pageContext.setAttribute(name, result);
pageContext.setAttribute("currentPage", currentPage);
} catch (Exception e) {
System.err.println("GoodsTypeTag.doEndTag ERROR ## "+e.getMessage());
}
return EVAL_PAGE;
}
}

GoodsInfoTag

package taglib;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import entity.Goods;
import services.GoodsServices;

public class GoodsInfoTag extends TagSupport{
private boolean isFirst = false;//是否为第一页
private boolean isLast = false;//是否为最后一页
private int recordsCount=0;//记录记录总数
private int currentPage;//记录当前页面
private final int recordsInOnePage = 6;//设置每页显示数目



private String type;//记录商品类型
private String name;
private String url;
public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public int doStartTag() throws JspException {
try {
GoodsServices goodsServices=new GoodsServices();
recordsCount=goodsServices.getRecordNumber(type);  //查询该类型商品数目
currentPage=(Integer)pageContext.getAttribute("currentPage"); //获取当前页号
//如果是第一页,设置isFirst标记变量
if (currentPage <= 0){
isFirst = true;
}
else {
isFirst = false;
}
//考虑最后是否是最后一页,分两种情况
//如果总记录数能够被每页记录数整除,页码为倒数第二页码即可作为最后一页
if (recordsCount % recordsInOnePage == 0
&& currentPage >= recordsCount / recordsInOnePage - 1) {
isLast = true;
}
//如果总记录数不能够被每页记录数整除,页码为倒数第一页码才作为最后一页
else if (recordsCount % recordsInOnePage != 0
&& currentPage >= recordsCount / recordsInOnePage) {
isLast = true;
}
else {
isLast = false;
}
} catch (Exception e) {
System.err.println("GoodsInfoTag.doStartTag ERROR ## "+e.getMessage());
}
return SKIP_BODY;
}

public int doEndTag() throws JspException {
try {
int count=0;
List<Goods> result=(List<Goods>)pageContext.getAttribute(name);
pageContext.getOut().print("<table  width='580px' border='0' align='center' cellpadding='0' cellspacing='0'>");
for (Goods goods : result) {
String image=goods.getGoodsPicture();
String name=goods.getGoodsName();
String goodsID=goods.getGoodsID();
if(count%3==0)  //换行
  pageContext.getOut().print("<tr>");
pageContext.getOut().print("<td width='33%'>");
pageContext.getOut().print("<table width='100%' border='0' cellpadding='0' cellspacing='0' bgcolor='#EBF8FE'>");
pageContext.getOut().print("<tr>");
pageContext.getOut().print("<td><img src='"+image+"' width=190 height='190'/></td>");
pageContext.getOut().print("</tr>");
pageContext.getOut().print("<tr align='center' valign='top' height='30px'>");
pageContext.getOut().print("<td><br><a href='GoodsDetailsServlet?goodsID="+goodsID+"'>");
pageContext.getOut().print(name);
pageContext.getOut().print("</a></td>");
pageContext.getOut().print("</tr>");
pageContext.getOut().print("</table>");
pageContext.getOut().print("</td>");
count++;
if(count%3==0)
pageContext.getOut().print("</tr>");
}
pageContext.getOut().print("</table>");

//页码导航超链标签
String pageInfos = "";
//如果不是第一页,得到“首页”和“上一页”的超链
if (!isFirst) {
pageInfos += "<a href="+url+"?pageNumber=0 >首页</a>  ";
pageInfos += "<a href="+url+"?pageNumber=" + (currentPage - 1)
+ ">上一页</a>  ";
}
//如果不是最后一页,得到“下一页”和“最后一页”的超链
int lastPage=(recordsCount % recordsInOnePage == 0?(recordsCount / recordsInOnePage - 1):(recordsCount / recordsInOnePage));
if (!isLast) {
pageInfos += "<a href="+url+"?pageNumber=" + (currentPage + 1)
+ ">下一页</a>  ";
pageInfos += "<a href="+url+"?pageNumber="
+ lastPage + ">最后一页</a>";
}
pageContext.getOut().print("<br><center>"+pageInfos+"</center>");
} catch (Exception e) {
System.err.println("GoodsInfoTag.doEndTag ERROR ## "+e.getMessage());
}
return EVAL_PAGE;
}


}

这是在网上下载的程序,研究了半天结果还是显示不出来,不知道是什么原因,求解啊~~~~~ --------------------编程问答-------------------- 在Web.xml中定义一个:

<jsp-config>
    <taglib>
        <taglib-uri>/mytags</taglib-uri>
        <taglib-location>/WEB-INF/mytags.tld</taglib-location>
    </taglib>
  </jsp-config>


页面这样引入:

<%@ taglib uri="/mytags" prefix="mytag"%>
--------------------编程问答--------------------
引用 1 楼 hzw2312 的回复:
在Web.xml中定义一个:
XML/HTML code?123456<jsp-config>    <taglib>        <taglib-uri>/mytags</taglib-uri>        <taglib-location>/WEB-INF/mytags.tld</taglib-location>    </taglib>  </jsp-conf……


还是不行啊。。。也不报错。。。真头疼
--------------------编程问答-------------------- 来个人啊~~~
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,