JSP自定义标签实例之:TagSupport

继承TagSupport是一个简单且实用也最常用的。
一、需求分析
应用中常用数据字典来管理常用数据。后台数据往往在处理数据实只认字典项的code.显然在页面上需要对其进行转换。以让用户能够认识。所以往往要对这个字典项的code进行翻译。所以可以用标签来实现。

二、标签处理类

public class DictNameTag extends TagSupport {
private AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
private DictManager dictManager = (DictManager)ctx.getBean("dictManager");

private String code;

public String getCode() {
return code;
}


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


@Override
public int doStartTag() throws JspException {
String dictName = "";
JspWriter out = this.pageContext.getOut();
if(code!=null && !"".equals(code)){
SystemDict dict = dictManager.getByCode(code,null);
if(dict!=null){
if(dict.getCodecontent()!=null)
dictName = dict.getCodecontent();
}
}
try {
out.println(dictName);
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}


@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return super.doEndTag();
}


}


三、tld注册

<tag>
<name>dictName</name>
<tag-class>com.harmony.dwyj.common.tag.DictNameTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>code</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>


四、页面应用:

<h:dictName code="${bean.eventCode}"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值