自定义标签

由于字典的缘故,当前表中数据存的是字典表的code,所以在页面显示的时候需要再次进行转换。于是我就想到了用标签来解决这个问题。

1.创建自己的标签

public class DisplayTag extends TagSupport{
   
    private String property = null;
    
    /**
     * 要显示的代码编号
     */
    private String code = null;
    
    @Override
    public int doStartTag() throws JspException {
        return SKIP_BODY;
    }
    
    @Override
    public int doEndTag() throws JspException {
        try {
            DicService dicservice = (DicService) SpringContextUtil.getBean("dicService");
            String result = dicservice.showDicByCode(property, code);
            if(StringUtils.isNotBlank(result)){
                pageContext.getOut().write(result);
            }else{
                pageContext.getOut().write("");
            }
        } catch (IOException e) {
            System.out.println("DisplayTag报错");
        }
        return EVAL_PAGE;
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) throws JspException {
        this.code = code;
    }
}

2.创建tld文件 mytag.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">
    <display-name>通过属性和code获取字典的值</display-name>
    <tlib-version>1.0</tlib-version>  
    <jsp-version>1.2</jsp-version>  
    <short-name>tags</short-name>
    <uri>/tags-mytag</uri>
    <tag>
        <name>mytag</name>
        <tag-class>com.talent.common.tag.DisplayTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>property</name>
            <required>true</required>
            <rtexprvalue>fasle</rtexprvalue>
        </attribute>
        <attribute>
            <name>code</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib> 
3.在web.xml文件中添加

<jsp-config>
      <taglib>
          <taglib-uri>/tags-mytag</taglib-uri>
          <taglib-location>/WEB-INF/tags/mytag.tld</taglib-location>
      </taglib>
  </jsp-config>


4.在jsp页面上引入:<%@ taglib uri="/tags-mytag" prefix="t" %>,并且可以用<t:mytag code="${xxx.other_titleType }" property="otherInfo"/>


经过测试 、el表达式当参数传入是没问题,如果有问题,就是你包没导入。
jsp-api、jstl-1.2.jar

如果复制上面代码,在web项目中肯定会出问题。因为在DisplayTag类中,是没法用@Autowired把service注入进去,

那么就需要一个工具类:SpringContextUtil.java
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextUtil implements ApplicationContextAware{
    
    private static ApplicationContext ctx;
    
    public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException {    
        ctx = applicationContext;    
    }
    public static ApplicationContext getApplicationContext(){  
        return ctx;  
    }  
    public static Object getBean( String beanName ) {    
        return ctx.getBean( beanName );    
    }    
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值