论坛首页 入门技术论坛

JSP自定义EL函数实例教程

浏览 1771 次
该帖已经被评为新手帖
作者 正文
   发表时间:2009-01-18   最后修改:2009-02-06

类:

com.yx.news.view.action.TaoFunction

 

package com.yx.news.view.action;

import com.yx.common.constant.SystemFiles;
import com.yx.common.util.GetSetInfo;
import com.yx.dao.bean.DAOFactory;
import com.yx.news.data.bo.TStyle;
import com.yx.news.model.ITStyleService;

public class TaoFunction {
    private static ITStyleService styleService =
        (ITStyleService) DAOFactory.getNoRegisterObject("styleService");
   
    public static  String t(String alias) {
        TStyle t = styleService.findByAlias(alias);
        //把标签ID作为文件名,写到项目的相应路径中
        String rootPath = GetSetInfo.getConfigValue(
                SystemFiles.SYSTEM_SETTING, "staticPath");
        if(t == null) {
            return null;
        } else if("00".equals(t.getType()) || "10".equals(t.getType())) {//系统标签或模板
            rootPath ="<jsp:include page=\"" + rootPath + "/mark/sys/" + t.getId() + ".jsp" + "\"/>";
        } else if("01".equals(t.getType()) || "11".equals(t.getType())) {
            rootPath ="<jsp:include page=\"" + rootPath + "/mark/def/" + t.getId() + ".jsp" + "\"/>";
        }
        return rootPath;
    }
}

 

 

tld文件:

Tao.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">
    <tlib-version>1.0</tlib-version>
    <short-name>Tao</short-name>
    <function>
        <description>用于连接两个字符串</description>
        <name>t</name>
        <function-class>
            com.yx.news.view.action.TaoFunction
        </function-class>
        <function-signature>
            java.lang.String t(java.lang.String)
        </function-signature>
        <example>${Tao:t(str1)}</example>
    </function>
</taglib>

 

调用:

<%@ taglib prefix="Tao" uri="/WEB-INF/tld/Tao.tld"%>

${Tao:t("一级栏目标签(ul)") }

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics