WEB-INF/tlds/app.tld中

定义一个文件。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYOE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"

    "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">

<taglib>

    <tlib-version>1.0</tlib-version>

    <jsp-version>1.2</jsp-version>

    <short-name>Application Tag Library</short-name>

    <description>This tag library contains functionality specific to the PICC CLAIM Application.</description>

    <!--下拉框列表-->

    <tag>

        <name>select</name>

        <tag-class>com.ct.framework.tag.SelectTag</tag-class>

        <attribute>

            <name>codeType</name>

            <required>true</required>

            <rtexprvalue>true</rtexprvalue>

            <description>代码类型</description>

        </attribute>

        <attribute>

            <name>language</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>language</description>

        </attribute>

        <attribute>

            <name>id</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>标签Id</description>

        </attribute>

        <attribute>

            <name>name</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>下拉列表name</description>

        </attribute>

        <attribute>

            <name>cssClass</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>下拉列表样式</description>

        </attribute>

        <attribute>

            <name>style</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>风格</description>

        </attribute>

        <attribute>

            <name>selectedValue</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>已选中的值</description>

        </attribute>

        <attribute>

            <name>multiple</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>是否可以多选</description>

        </attribute>

        <attribute>

            <name>withAll</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>是否显示全部</description>

        </attribute>

        <attribute>

            <name>withBlack</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>是否显示空白</description>

        </attribute>

        <attribute>

            <name>condition</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>父级列表(筛选条件)</description>

        </attribute>

        <attribute>

            <name>onchange</name>

            <required>false</required>

            <rtexprvalue>true</rtexprvalue>

            <description>onchange事件</description>

        </attribute>

</tag>

在com.ct.framework.tag中有SelectTag.java

public class SelectTag extends TagSupport{

    private final Log logger = LogFactory.getLog(this.getClass());

    private String codeType;

    private String language;

    private String id;//标签id

    private String name;//下拉列表名字

    private String cssClass;//下拉列表样式

    private String style;//风格

    private String selectedValue;//已选中的值

    private Boolean multiple;//是否可以多选

    private Boolean withAll;//是否显示全部

    private Boolean withBlack;//是否显示空白

    private String codition;//父级列表(筛选条件)

    private String onchange;//onchange事件

 

    public int doStartTag() throws JspException{

        if(language == null){

            language = "C";

        }

        HttpSession httpSession = this.pageContext.getSession();

        StringBuffer resultBuffer = new StringBuffer();

        ConditionVo conditionVo = new ConditionVo();

        JspWriter jspWriter = pageContext.getOut();

        CodeCodeService codeCodeService =

                    (CodeCodeService)ServiceFactory.getService("codeCodeService");

        List<CodeCode> codeCodeList;

        conditionVp.setSearchCondition(condition);

        try{

            //这步用type查询出类型下的code集合对象

            codeCodeList = codeCodeService.listCodeSelect(codeType,conditionVo,httpSession);

        }catch(Exception ex){

            ...

        }

        //标签的处理

        resultBuffer.append("<select ");

        if(StringUtiles.isNotEmpty(name)){

            resultBuffer.append(" name=\"").append(name).append("\"");

        }

        //id

        if(StringUtils.isNotEmpty(id)){

            resultBuffer.append(" id='"").append(id).append("\"");

        }

        //css样式

        if(StringUtils.isEmpty){

            

        }else{

            resultBuffer.append(" class=\"").append(cssClass).append("\"");

        }

        //style

        if(StringUtils.isNotEmpty(style)){

             resultBuffer.append(" style=\"").append(style).append("\"");

        }

        //多选

        if(multiple!=null&&multiple){

            resultBuffer.append(" multiple");

        }

        //onchange事件    

        if(StringUtils.isNotEmpty(onchange)){

             resultBuffer.append(" onchange=\"").append(onchange).append("\"");

        }

        resultBuffer.append(">");

        //列表项中显示全部

        if(withAll!=null&&withAll){

            resultBuffer.append("<option value=''>");

            resultBuffer.append(请选择);

            resultBuffer.append("</option>);

        }

        for(CodeCode codeCode:codeCodeList){

            String codeCodeStr = codeCode.getCodeCode();

            String codeCNameStr = codeCode.getCodeCName();

            resultBuffer.append("<option");

            resultBuffer.append(" value=\"" + codeCodeStr + "\"");

            if(selectedValue!=null&&selectedValue.equals(codeCodeStr)){

                resultBuffer.append(" selected=\"selected\"");

            }

            resultBuffer.append(">");

            resultBuffer.append(codeCNameStr);

            resultBuffer.append("</option>");

        }

        resultBuffer.append("</select>");

        try{

            logger.debug("SelectTag标签:" + resultBuffer.toString());

            jspWriter.print(resultBuffer.toString());

        }catch(Exception ex){

            ...

        }

        return EVAL_BODY_INCLUDE;

    }

    public int doEndTag()throws Exception{

        this.codeType="";

        this.language="";

        this.id="";

        this.name="";

        this.cssClass="";

        this.selectedValue="";

        this.multiplie=false;

        this.withAll=false;

        this.withBlack=false;

        this.condition="";

        this.onchange="";

        return EVAL_PAGE;

    }

setter getter方法

}

 

Spring注入Service实例工厂

public class ServiceFactory{

    private static WebApplicationContext context;

    private ServiceFactory(){}

    public static void initServiceFactory(ServletContext servletContext){

        context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

    }

    public static Object getService(String serviceName){
        return context.getBean(serviceName);

    }

}