zoukankan      html  css  js  c++  java
  • 如何在页面中引用自定义标签

    项目中用到了自定义标签,在此记录一下,感觉这套体系可能有点老了,不过毕竟自己用过了,从别人那里学到了,还是记录下来,难免以后会用到。

    首先,先定义好函数

     1 package org.controller.taglib;
     2 
     3 import java.util.List;
     4 
     5 import org.apache.commons.lang.ArrayUtils;
     6 import org.apache.commons.lang.StringUtils;
     7 import org.apache.commons.lang.math.NumberUtils;
     8 import org.controller.cfg.Settings;
     9 import org.controller.cfg.SystemGlobals;
    10 import org.model.businessobject.bbs.privilege.Employee;
    11 import org.model.businessobject.enumpo.ApplyState;
    12 import org.model.businessobject.enumpo.ApplyVacationState;
    13 import org.model.businessobject.workflow.Apply;
    14 import org.model.businessobject.workflow.FeeType;
    15 import org.model.businessobject.workflow.DeptBudget.MonthBudget;
    16 import org.model.businessobject.workflow.vacation.ApplyVacation;
    17 import org.model.businessobject.workflow.vacation.VacationType;
    18 import org.model.locator.impl.ServiceLocatorImpl;
    19 import org.model.service.ApplyService;
    20 import org.model.util.DES;
    21 import org.model.util.DataUtils;
    22 
    23 /**
    24  * 作者:wyh
    25  * 描述:定制fn 函数
    26  * 说明:
    27  */
    28 public final class CustomFunctions {
    29 /**
    30      * 通过员工id 反查显示员工名
    31      * @param empId
    32      * @return
    33      */
    34     public static String  renderEmpName(final int empId){
    35         String empName = StringUtils.EMPTY;
    36         if(empId>0){
    37             final Employee emp = ServiceLocatorImpl.newInstance().getPrivilegeService().getEmployeeById(empId);
    38             if(emp!=null) {
    39                 empName = emp.getEmpName();
    40             }
    41         }
    42         return empName;
    43     }
    44 }

    第二步,就是定义标签了,标签文件是tld格式的文件

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
     3   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
     5   version="2.0">
     6   <description>custom functions library</description>
     7   <display-name>custom functions</display-name>
     8   <tlib-version>1.1</tlib-version>
     9   <short-name>myfn</short-name><!--定义标签头部-->
    10   <uri>http://www.xxx.com/jsp/tag/functions</uri> <!--这块引用的时候会用到-->
    11 <function>
    12       <description>通过员工Id显示员工名称</description>
    13       <name>renderEmpName</name>
    14       <function-class>org.controller.taglib.CustomFunctions</function-class>
    15       <function-signature>java.lang.String  renderEmpName(int)</function-signature>
    16   </function>

    接下来,把tld文件加入jspf中,多个标签直接依次加入就好

    <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%><%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%><%@ taglib uri="http://www.xxx.com/jsp/roco-tag" prefix="roco"%><%@ taglib uri="http://www.xxx.com/jsp/tag/functions" prefix="myfn"%>

    到此,就可以在页面中引用了,只需在页头加入<%@ include file="/WEB-INF/pages/public/taglibs.jspf"%>即可。

    当要使用这个标签时,跟jstl引用方法是一样的,上面那个例子就这样用:

    <td>${myfn:renderEmpName(applyVacation.empId)}</td>

    总之来说,自定义标签很简单,知道怎么用就行了

  • 相关阅读:
    SQL中distinct的用法
    python requests 高级用法 -- 包括SSL 证书错误的解决方案
    odoo js
    线程池的理解及使用
    单点登录原理及简单实现
    如何重写hashCode()和equals()方法
    多线程中的Lock小结
    Sql语句的优化摘要
    Spring事务管理
    并发编程之原子性、可见性、有序性的简单理解
  • 原文地址:https://www.cnblogs.com/wyhailjn/p/3792874.html
Copyright © 2011-2022 走看看