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

[/code]其中存在两张表A和B,其中表B中字段的值是表A的某个字段,现在给你B中某字段的值和对象A,要你取出它在对象A中对应的属性值。在这种情况下你若选择使用反射机制会非常方面。一下是相应的代码:
[code="java"]

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值