JSTL自定义函数,配合c标签使用

      我们在使用jstl标签的时候,有的时候c标签和jstl标签提供的函数无法满足需求,就需要我们自定义一些JSTL函数,比如我想实现根据数据库保存的用户的状态标志(1或者0),展示到页面是“启用”或者“禁用”的文字。这里我们用自定义函数来实现。

实现分成以下几部:

1、  编写Java类,实现自定义函数的功能要求;

    要求所有的方法必须为static方法。

2、  编写tld文件;

     需要将tld文件放到WEB-INF目录或子目录下;

3、  配置web.xml文件,注册tld标签;

4、  jsp页面使用JSTL自定义函数;


详细步骤讲解:

1、  编写Java类,实现自定义函数的功能要求:

    此Java类的getDisabled(int)实现了根据用户的标志位判断用户的状态是启用或禁用状态。

package com.yimian.springdemo.utils;

import com.yimian.springdemo.utils.enums.EnumDISABLED;

/**
 * 自定义JSTL函数   
 * @author dell
 */
public class JSTLUtils {
	
	/**
	 * 启用/禁用 的状态
	 * @param disabled
	 * @return
	 */
	public static String getDisabled(int disabled){
		if(disabled==EnumDISABLED.YES.getKey()){
			return EnumDISABLED.YES.getValue();
		}else if(disabled==EnumDISABLED.NO.getKey()){
			return EnumDISABLED.NO.getValue();
		}else{
			return "";
		}
	}
}

package com.yimian.springdemo.utils.enums;

/**
 * 启用/禁用
 * @author dell
 *
 */
public enum EnumDISABLED {
	
	YES(1,"启用"),NO(0,"禁用");
	
	private final int key;
	private final String value;
	
	EnumDISABLED(int key,String value){
		this.key = key;
		this.value = value;
	}
	
	public int getKey(){
		return this.key;
	}

	public String getValue(){
		return this.value;
	}
}


2、   编写tld文件:

    注意事项:

    short-name : 没有什么意义

    uri : 自定义,与jsp页面引用自定义标签的时候uri地址是一样的,例如:

    <%@ taglib prefix="fns" uri="http://fns.com"%>

 

    function部分定义的是函数部分:

    name : 自定义,没有什么具体含义

    function-class : 实现方法的Java类

    function-signature : 注册的具体方法,格式如 

       返回值类型 方法名(参数)      java.lang.String getDisabled(int)

       注意事项:如果参数是int,可以直接使用,如果是StringInteger等对象类型,需要使用java.lang.Stringjava.lang.Integer等方式。

    example : 使用例子参考,可以不写


fns.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">
	
	<tlib-version>1.0</tlib-version>
	<short-name>fnseeeee</short-name>
	<uri>http://fns.com</uri>
	 
	<function>
	 	<name>getDisabled</name>
	 	<function-class>com.yimian.springdemo.utils.JSTLUtils</function-class>
	 	<function-signature>java.lang.String getDisabled(int)</function-signature>
	 	<example>${fns:getDisabled(key)}</example>
	</function>
</taglib>

3、  配置web.xml文件,注册tld标签:

        taglib-uri : 与tld文件中的uri名称一致

        taglib-location: tld文件的位置,需要将tld文件放到WEB-INF文件夹或子文件夹下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<!-- 加载web容器的时候,加载的配置文件的信息 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/spring-context.xml,/WEB-INF/spring/mybatis-context.xml</param-value>
	</context-param>

	<!-- 加载web容器启动的时候,自动加载ApplicationContext中的配置信息 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- DispatcherServlet -->
	<servlet>
		<servlet-name>appServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring/servlet-context.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>appServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<!-- 注册JSTL函数 -->
	<jsp-config>
		<taglib>
			<taglib-uri>http://fns.com</taglib-uri>
			<taglib-location>/WEB-INF/fns.tld</taglib-location>
		</taglib>
	</jsp-config>
</web-app>

4、  jsp页面使用JSTL自定义函数:

    引入JSTL标签,注意这里是根据uri地址来找到具体的tld文件的,需要和web.xml中一致。

    <!-- 导入自定义jstl函数 -->  

    <%@ taglib prefix="fns" uri="http://fns.com" %>

 

    页面引用自定义函数的具体形式如下:

    <td>${fns:getDisabled(po.disabled)}</td>


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值