当前位置: 代码迷 >> JavaScript >> 在jsp中施用自定义标签 实现反射机制
  详细解决方案

在jsp中施用自定义标签 实现反射机制

热度:234   发布时间:2012-10-27 10:42:26.0
在jsp中使用自定义标签 实现反射机制
其中存在两张表A和B,其中表B中字段的值是表A的某个字段,现在给你B中某字段的值和对象A,要你取出它在对象A中对应的属性值。在这种情况下你若选择使用反射机制会非常方面。一下是相应的代码:

import java.io.IOException;
import java.lang.reflect.Field;

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

public class RFTag extends TagSupport {

	private static final long serialVersionUID = -2360561035810220875L;

	private Object object;
	
	private String property;

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}
	
	public int doStartTag() throws JspException {
		
		JspWriter out = this.pageContext.getOut();

		try {
			if (object == null) {
				out.println("");
			} else {
//get the object of this field
				Field f = object.getClass().getDeclaredField(property);
				f.setAccessible(true);
//get the value of this field
				Object value = f.get(object);
				out.println(value == null ? "" : value.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
		return SKIP_BODY;
	}

}

下面是WEB-INF目录下的文件fs.tld文件的内容,主要是定义的在jsp中使用的标签。代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd" >
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>2.0</jsp-version>
  <short-name>foundTag</short-name>
  <uri>http://www.foundersoftware.com/founder/tags</uri>
  <tag>
    <name>page</name>
    <tag-class>com.founder.abgent.core.web.tag.PageTag</tag-class>
    <attribute>
  		<name>action</name>
  		<required>true</required>
  		<rtexprvalue>true</rtexprvalue>
  	</attribute>
  	 <attribute>
  		<name>param</name>
  		<required>false</required>
  		<rtexprvalue>true</rtexprvalue>
  	</attribute>
  	  <attribute>
  		<name>color</name>
  		<required>false</required>
  	</attribute>
  </tag>
  
  <tag>
  	<name>rf</name>
  	<tag-class>com.founder.bcimp.core.web.tag.RFTag</tag-class>
  		<attribute>
  			<name>object</name>
  			<required>true</required>
  			<rtexprvalue>true</rtexprvalue>
  		</attribute>
  		<attribute>
  			<name>property</name>
  			<required>true</required>
  			<rtexprvalue>true</rtexprvalue>
  		</attribute>
  </tag>
</taglib>



具体的在jsp中的应用实例.代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.foundersoftware.com/founder/tags" prefix="fs"%>

<div>
	<c:if test="${not empty enterpriseCustomFieldList}">
		<table>
			<tr>
				<td colspan="2"><fmt:message key="crs.compound.enterprise.custom.field" /></td>
			</tr>
			<c:forEach items="${enterpriseCustomFieldList}" var="enterpriseCustomField">
				<tr>
					<th>${enterpriseCustomField.columnLabel }</th>
					<td><input type="text" name="${enterpriseCustomField.columnName}" maxlength="${enterpriseCustomField.length}" value[b]="<fs:rf object="${compound}" property="${enterpriseCustomField.columnName}" />"/[/b]></td>
				</tr>
			</c:forEach>
		</table>
	</c:if>
</div>
  相关解决方案