自定义函数标签:实现前台小数据的获取

今天小编用自定义函数标签来实现前台导航栏的管理
自定义函数标签优点:操作简单,方便快捷 缺点:只能用于小数据的获取
首先:要将文件放在src下并且以dlt 具体操作如下:
dao:	/**
	 * 查询所有种类
	 * 自定义函数标签调用:必须是public、static
	 * @return
	 * @throws SQLException
	 */
	public static List<Category> findAllCategories() throws SQLException{
		String sql = "select * from category";
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		return qr.query(sql, new BeanListHandler<Category>(Category.class));
	}
tld:<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
	version="2.0">

	<description>JSTL 1.1 functions library</description>
	<display-name>JSTL functions</display-name>
	<tlib-version>1.1</tlib-version>
	<!--前台引用prefix后的名字  -->
	<short-name>myfn</short-name>
	<!--自己定义的前台引用时的路径  -->
	<uri>http://www.yinheedu.com</uri>

	<function>
	<!-- 说明标签,为此方法添加注释一般的存在  -->
		<description>
     		select all categories
   		</description>
		<name>findAllCategories</name><!--前台el表达式运行方法的名字--!>
		<!--标签的实现类--!> 
		<function-class>com.yinhe.dao.CategoryDao</function-class>
		<!--标签的输入和输出参数--!>
		<function-signature>java.util.List findAllCategories()</function-signature>
	</function>

</taglib>
前台;引用<%@taglib prefix="myfn" uri="http://www.yinheedu.com"%>
遍历;<c:forEach var="category" items="${myfn:findAllCategories()}"(遍历时的el表达式 写标签名时要带小括号)
varStatus="status">
<c:choose>
<c:when test="${status.first}">
<li class="active"><a href="product_list.jsp">${category.cname}<span
class="sr-only">(current)</span></a></li>
</c:when>
<c:otherwise>
<li><a href="product_list.jsp">${category.cname}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值