Struts自定义标签

1.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.2</jsp-version> 
<short-name>saasCustomTag</short-name> 

<!--根据ID得到名称 by YJ--> 
<tag> 
<name>getProperty</name> 
<tag-class>com.zte.ict.soc.commom.customTag.GetPropertyTag</tag-class> 
<body-content>empty</body-content> 
<!-- POJO类名 -->
<attribute> 
	<name>pojo</name> 
	<required>ture</required> 
	<rtexprvalue>true</rtexprvalue> 
</attribute> 
<!-- PID属性名 -->
<attribute>
	<name>pidName</name> 
	<required>ture</required> 
	<rtexprvalue>true</rtexprvalue> 
</attribute> 
<!-- PID属性值 -->
<attribute> 
	<name>pidValue</name> 
	<required>ture</required> 
	<rtexprvalue>true</rtexprvalue> 
</attribute>
<!-- 名称属性 -->
<attribute> 
	<name>getName</name> 
	<required>ture</required> 
	<rtexprvalue>true</rtexprvalue> 
</attribute> 
</tag> 

</taglib> 

2.对应类
/**
 * 
 */
package com.zte.ict.soc.commom.customTag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.zte.ict.soc.commom.service.IGetPropertyService;



/**
 * @author YJ
 * Feb 1, 2010
 * 示例:
 *
 *jsp: 
 *<%@ taglib uri="/WEB-INF/tld/saasCustomTag.tld" prefix="saasCustomTag"%>
 *
 *显示父ID对应名称:
 *<saasCustomTag:getProperty pojo="AssetPhysicalArea" pidName="objId" pidValue="${bean.PObjId}" getName="areaName"/>
 *
 *显示人员ID对应名称:
 *<saasCustomTag:getProperty pojo="User" pidName="objID" pidValue="${bean.princaipalUserObjId}" getName="userName"/>
 *
 *END示例;
 *
 *说明:传4个参数--pojo(POJO类名),pidName(类ID属性名),pidValue(类ID值),getName(所取属性名称),    返回该名称的值
 */
public class GetPropertyTag extends TagSupport {

	/**
	 * POJO类名
	 */
	public String pojo;
	/**
	 * ID属性名
	 */
	public String pidName;
	/**
	 * ID属性值
	 */
	public String pidValue;
	/**
	 * 属性名称
	 */
	public String getName;
	
         //get , set 方法略

	@Override
	public int doStartTag() throws JspException {
		doService();//这里还可以做下异常处理
		return EVAL_PAGE; 
	}

	private void doService() {
		ApplicationContext ac
		=WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());
		IGetPropertyService service = (IGetPropertyService) ac.getBean("getPropertyService");
		try {
			String name = service.getProperty(pojo, pidName, pidValue, getName);
			pageContext.getOut().write(name);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	@Override
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}
	
}


DAO,SERVICE类,及spring中配置略

你可能感兴趣的:(spring,Web,jsp,struts,servlet)