📄 j-customtags-4-2.html
字号:
return test;
}
public void setTest(String string) {
test = string;
}
}
</code>
</pre>
<p>
改写后的标签可以将 bean 属性作为 map 中的项使用,像下面这样:
</p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
<jsp:useBean id="bean" class="trivera.tags.map.Test"/>
<map:mapDefine id="employee2">
<map:mapEntry id="firstName" name="bean" property="test"/>
<map:mapEntry id="age" value="33" type="java.lang.Integer"/>
<map:mapEntry id="salary" value="22.22" type="java.lang.Float"/>
<map:mapEntry id="properties" name="properties" />
</map:mapDefine>
</code>
</pre>
<p>
注意 <code>firstName</code> 项现在是用 bean 的 test 属性定义的。</p>
<p>
为了做到这一点,我们需要在自定义标签中加入 Relection,像这样(参看代码中的注释以了解改变的过程):
</p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
public class MapEntryTag extends TagSupport {...
/* All of these have corresponding getters and setter method */
String type = "java.lang.String"; //Holder for the type attribute
String id; //Holder for the id attribute
String value; //Holder for value attribute
String name; //Holder for name attribute
String property; //Holder for property attribute
String scope; //Holder for scope attribute
public int doEndTag() throws JspException {
MapDefineTag mapDef = (MapDefineTag) this.getParent();
Object objectValue = null;
/* Check to see if the value property is set,
if it is then this is a simple entry */
if (value !=null){
if (type.equals("java.lang.String")) {
objectValue = value;
} else if (type.equals("java.lang.Integer")) {
Integer intValue = Integer.valueOf(value);
objectValue = intValue;
} else if (type.equals("java.lang.Float")) {
Float floatValue = Float.valueOf(value);
objectValue = floatValue;
}
/* If it is not a simple entry,
then use reflection to get the property from the bean */
}else {
Object bean =null;
if (scope == null){
bean = pageContext.findAttribute(name);
}else if("page".equalsIgnoreCase(scope)){
bean = pageContext.getAttribute(name);
}else if("request".equalsIgnoreCase(scope)){
bean = pageContext.getRequest().getAttribute(name);
}else if("session".equalsIgnoreCase(scope)){
bean = pageContext.getSession().getAttribute(name);
}else if("application".equalsIgnoreCase(scope)){
bean = pageContext.getServletContext().getAttribute(name);
}
/* If the property attribute is null,
then just use the bean as the entry*/
if (property==null){
objectValue = bean;
mapDef.getMap().put(id,bean);
/* If the property attribute is set,
then use reflection to read the property */
}else {
try{
String propertyMethod = "get" +
property.substring(0,1).toUpperCase() +
property.substring(1, property.length());
Method prop = bean.getClass()
.getMethod(propertyMethod,new Class[]{});
objectValue = prop.invoke(bean, new Object[]{});
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
mapDef.getMap().put(id,objectValue);
return EVAL_PAGE;
}
</code>
</pre>
<p>
看起来仅仅是为了实现一些很多标签都要有的功能就需要做大量的工作。幸运的是,有一个库使这种开发变得容易了。在下一小节分析这个库 —— Struts —— 是如何发挥作用的。
</p>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index4.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-customtags-7-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-customtags-4-1.html"><img alt="上页" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-customtags-4-3.html"><img alt="下页" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">关于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律条款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">联系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
<script src="//www.ibm.com/common/stats/stats.js" language="JavaScript1.2" type="text/javascript"></script>
<noscript>
<img border="0" alt="" height="1" width="1" src="//stats.www.ibm.com/rc/images/uc.GIF?R=noscript"></noscript>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -