struts2自定义标签

1.编辑tld文件


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<!-- a tag library descriptor -->
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>domaindata</shortname>
<uri />
<info>access contro</info>
<tag>
<name>domaindataField</name>
<tagclass>com.best1.wms.webapp.tag.DaomaindataTag</tagclass>
<attribute>
<name>parentcode</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue> <!-- 代表可直接指定或计算获得 -->
</attribute>
<attribute>
<name>code</name>
<required>true</required>
</attribute>
</tag>
</taglib>




2.编辑解析xml文件的java文件


package com.best1.wms.webapp.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.best1.wms.common.exception.DaoException;
import com.best1.wms.common.exception.ServiceException;
import com.best1.wms.webapp.utils.LableUtil;

public class DaomaindataTag extends BodyTagSupport{
private String parentcode;
private String code;


public String getParentcode() {
return parentcode;
}

public void setParentcode(String parentcode) {
this.parentcode = parentcode;
}

public String getCode() {
return code;
}

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

private static final long serialVersionUID = 1L;

@Override
public int doStartTag() throws JspException {
return EVAL_BODY_AGAIN;
}

@Override
public int doEndTag() throws JspException {

try {
try {
// pageContext.getOut().write(LableUtil.getcsLable(parentcode, code));
pageContext.getOut().write(LableUtil.getDomaindataLable(parentcode, code)); //查询数据库获取值

} catch (ServiceException e) {
e.printStackTrace();
} catch (DaoException e) {
e.printStackTrace();
}

} catch (IOException e) {
throw new JspTagException("错误");
}

return EVAL_PAGE;
}
}



3.web.xml引入自定义的标签库


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


4.jsp页面引用


<%@ taglib prefix="dom" uri="/tld/domaindataTag" %>
<dd:domaindataField parentcode="5029" code="5599"></dd:domaindataField>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值