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>

你可能感兴趣的:(struts2)