自定义JSTL函数
来源:互联网 发布:福州淘宝模特 编辑:程序博客网 时间:2024/05/17 01:30
一、JSTL标签的使用
首先,让我们先来了解一下JSTL标签的使用。
(1)标签库的配置
将jstl.jar和standard.jar拷贝到WEB-INF/lib下(如果使用el表达式,不用拷贝这两个jar)
注意:JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat 4.x。
(2)标签库的使用
采用taglib指令引入,如:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
二、JSTL的优点
为什么使用JSTL,JSTL又有哪些优点?
(1)在应用程序服务器之间提供了一致的接口,最大程序地提高了WEB应用在各应用服务器之间的移植。
(2)简化了JSP和WEB应用程序的开发。
三、自定义标签库
许多(web?)开源框架都提供了大量的标签库(taglib)。但面对各种各样、永无止境的需求,实际应用中总会出现我们需要、但暂时无法找到或者没有提供的一系列标签,由此,自定义标签和函数应运而生。
对于自定义标签,《JSP2的自定义标签》,以供参考。
自定义标签库是一种非常优秀的表现层组件技术。通过使用自定义标签库,可以在简单的标签中封装复杂的功能。
当我们在JSP页面使用一个简单的标签时,底层实际上由标签处理类提供支持,从而可以使用简单的标签来封装复杂的功能,从而使团队更好地协作开发(能让美工人员更好地参与JSP页面的开发)。
四、自定义函数库
自定义函数,简单的讲,其实就是我们写的一些类的方法操作,需要在页面中调用。
下面以OA中出现的自定义JSTL函数为例。OA中在两处用到了自定义JSTL函数,一处是下面的例子,需要在页面中进行权限的即时认证,一处是结合Freemarker和JSTL函数,动态显示表单。
业务介绍:在JSP页面上,调用JSTL函数做即时认证,只有通过认证之后,才显示相应的(功能)按钮/菜单/输入框等。
1.定义函数类(方法为public static)
/** * JSTL函数,主要功能是可以完成权限的即时认证 * @author Administrator * */public class SecurityFunctions {private static AclManager aclManager;public static boolean hasPermission(int userId,String resourceSn,int permission){return aclManager.hasPermissionByResourceSn(userId, resourceSn, permission);}//这个方法不能定义为static,因为这将导致spring无法注入public void setAclManager(AclManager aclManager) {SecurityFunctions.aclManager = aclManager;}}
2.定义tld文件:my.tld,并放到WEB-INF下
<?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"> <tlib-version>1.0</tlib-version> <short-name>my</short-name> <uri>http://www.bjsxt.com/oa/functions</uri> <function> <name>hasPermission</name> <function-class>com.bjsxt.oa.web.SecurityFunctions</function-class> <function-signature>boolean hasPermission(int, java.lang.String,int)</function-signature> </function></taglib>
3.修改web.xml文件,添加:
<jsp-config> <taglib> <taglib-uri>http://www.bjsxt.com/oa/functions</taglib-uri> <taglib-location>/WEB-INF/my.tld</taglib-location> </taglib> </jsp-config>
4.JSP页面引入
<%@ taglib prefix="my" uri="http://www.bjsxt.com/oa/functions" %>
注意:uri在web.xml和jsp页面引入需要一致;
web.xml中的uri可以与location一致,也可以与tld文件中的uri一致,一般三者的uri一致
5.JSP页面使用
简单示例:
${my:testJstlFunction('teststring')}
上述JSTL函数使用示例:
<c:if test="${my:hasPermission(login.id,'person',3) }"><a href="#" onclick="del('person.do?method=del&id=${person.id }');">删除</a></c:if>
- 自定义jstl函数、自定义jstl标签
- 自定义JSTL函数
- 自定义JSTL函数
- 自定义JSTL函数
- 自定义JSTL函数
- 自定义JSTL函数
- jstl自定义函数
- 自定义JSTL函数
- 自定义JSTL函数
- jstl-自定义函数
- 自定义JSTL函数
- JSTL自定义函数标签
- jstl自定义函数
- 自定义JSTL函数标签
- jstl自定义函数
- jstl自定义函数
- jstl自定义函数
- JSTL 自定义Function函数标签
- struts2.1.8+hibernate3.6.0+spring2.5三大框架整合demo
- 深入理解const char*p,char const*p,char *const p,const char **p,char const**p,char *const*p,char**const p
- Runnable 并不一定是新开一个线程
- 物联网的一些基础的东西
- chown -R david /home/david改变文件属主为david
- 自定义JSTL函数
- const 修饰
- Java复习 9.1问题???
- PowerDeisger使用心得〔MySql〕
- Linux ALSA声卡驱动之八:ASoC架构中的Platform
- android 开发之路-------界面布局及相关文件配置说明
- 鸟哥的私房菜——总结(二)
- hdu1713 相遇周期
- java的取模 取余