您的位置:首页 > 其它

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

2017-10-05 16:24 453 查看
今天小编用自定义函数标签来实现前台导航栏的管理

自定义函数标签优点:操作简单,方便快捷 缺点:只能用于小数据的获取

首先:要将文件放在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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐