简单标签 SimpleTagSupport示例

最近处理JSP页面,需要把数据库查到的原始值,根据数据字典转换成更加直观的值。比如查到的结果是 01,jsp页面展示‘身份证’。

如果值比较少,就直接用c:if标签处理了,无奈接触的值比较多,只想到了自定义标签来处理,如果哪个大神有更好的办法,请赐教。

新建class,继承SimpleTagSupport类,实现doTag方法。

package com.jeecg.tool.variable;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HdDicTag extends SimpleTagSupport {

	private String dictionary;

	@Autowired
	private static SystemService systemService;

	/**
	 * @Title: getDictionary <BR>
	 * @Description: please write your description <BR>
	 * @return: String <BR>
	 */
	public String getDictionary() {
		return dictionary;
	}

	/**
	 * @Title: setDictionary <BR>
	 * @Description: please write your description <BR>
	 * @return: String <BR>
	 */
	public void setDictionary(String dictionary) {
		this.dictionary = dictionary;
	}


	@Override
	public void doTag() throws JspException, IOException {
		StringWriter writer = new StringWriter();
		JspFragment jspBody = this.getJspBody();
		jspBody.invoke(writer);
		String content = writer.toString();

		systemService = ApplicationContextUtil.getContext().getBean(SystemService.class);
		String sql = "SELECT
" + "	TYPENAME 
" + "FROM
" + "	"T_S_TYPE" 
" + "WHERE
"
				+ "	TYPECODE = '" + content + "'	AND TYPEGROUPID IN (
" + "SELECT
" + "	id 
"
				+ "FROM
" + "	T_S_TYPEGROUP g 
" + "WHERE
" + "	g.TYPEGROUPCODE = '" + dictionary + "')";
		List<Map<String, Object>> list = systemService.findForJdbc(sql);
		System.out.println(sql);
		String typename = "";
		if (list.size() > 1) {
			this.getJspContext().getOut().write("数据字典获取异常,不唯一");
			return;
		} else if (list.size() < 1) {
			this.getJspContext().getOut().write("");
		} else {
			typename = (String) list.get(0).get("TYPENAME");
			this.getJspContext().getOut().write(typename);
		}
	}

}

  WEB-INF下新建tld文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE 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.0</jsp-version>
    <short-name>t</short-name>
    <uri>hdDic-tags</uri>
    <display-name>"自定义标签"</display-name>
    
    <tag>
        <name>HdDicTag</name>
        <tag-class>com.jeecg.tool.variable.HdDicTag</tag-class>
        <body-content>JSP</body-content>
        <description>列表字段</description>
        <body-content>scriptless</body-content><!--这里要用这个处理-->
        <attribute>
            <name>dictionary</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
            <description>数据字典组编码 或 自定义字典</description>
        </attribute>
    </tag>
    
</taglib>

然后在页面引入、使用。

<%@ taglib prefix="hd" uri="hdDic-tags"%>
<hd:HdDicTag dictionary="cardType">${hdBhApplyPage.idCardCategory}</hd:HdDicTag>
原文地址:https://www.cnblogs.com/tyrone2005/p/10472075.html