我目前正在进行 struts 升级(从 struts 1.x 迁移到 2.x)
我的项目有一个自定义标记处理程序类,用于在应用程序中格式化数字
顶级域名文件
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>f</shortname>
<uri>http://jakarta.apache.org/struts/tags-html</uri>
<tag>
<name>formatNumber</name>
<tagclass>com.taghandler.FormatNumberTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>property</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>scope</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>format</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
FormatNumberTag 类
public class FormatNumberTag extends TagSupport
{
protected String name = null;
protected String property = null;
protected String scope = null;
protected String format = null;
//getters and setters of above member variables
public int doStartTag() throws JspException
{
// Look up the requested bean (if necessary)
Object bean = null;
if (RequestUtils.lookup(pageContext, name, scope) == null)
{
return (SKIP_BODY); // Nothing to output
}
// Look up the requested property value
Object value = RequestUtils.lookup(pageContext, name, property, scope);
if (value == null)
{
return (SKIP_BODY); // Nothing to output
}
String output = null;
if (format.equalsIgnoreCase(MyConstants.PRICE_FORMAT))
{
output = CustomConverter.priceFormat(value); //custom class which formats number
}
else if (format.equalsIgnoreCase(MyConstants.PERCENTAGE_FORMAT))
{
output = CustomConverter.positionFormat(value); //custom class which formats number
}
ResponseUtils.write(pageContext, output);
// Continue processing this page
return (SKIP_BODY);
}
}
JSP
<f:formatNumber name="AccountBean" property="floatingRate" format="percentage" />
这里,AccountBean 是 bean,floatingRate 是属性,百分比是格式。
1) 在上面的标记处理程序类中,使用了 ( org.apache.struts.util.RequestUtils )RequestUtils.lookup 和 ( org.apache.struts.util.ResponseUtils )ResponseUtils.write 方法,它们是 struts1 库的一部分。
- struts 2中要使用的相应方法和类是什么?
2)在JSP中,3个值/属性(accountBean,floatingRate,percentage)在自定义标签中传递。
如何将属性传递给struts 2中的自定义标签?
如何将 bean、属性和格式传递给 struts 2 中的自定义标签?请提供一个例子