struts2自定义标签

来源:互联网 发布:淘宝钻石展位怎么使用 编辑:程序博客网 时间:2024/05/23 19:31
1.首先在WEB-INF目录下建立文件itags.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>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>i</short-name>
<uri>/itags</uri>
<display-name>test tag</display-name>
<tag>
  <name>NumTag</name>
  <tag-class>com.zhou.common.tag.NumToUpper</tag-class>
  <attribute>
    <name>num</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
  </attribute> 
       </tag>
</taglib>
2.建立类com.zhou.common.tag.NumToUpper,类需继承TagSupport.


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


public class NumToUpper extends TagSupport {


private static final long serialVersionUID = -6571526129939017321L;


private final static String[] STR_NUMBER = { "零", "一", "二", "三", "四", "五",
"六", "七", "八", "九" };


/** 定义数组存放位数的大写 */
private final static String[] STR_MODIFY = { "", "十", "百", "千", "万", "十",
"百", "千", "亿", "十", "百", "千" };


public String num;


public String getNum() {
return num;
}


public void setNum(String num) {
this.num = num;
}


@Override
public int doEndTag() throws JspException {
String strInteger = num;
strInteger = new StringBuffer(strInteger).reverse().toString();
StringBuffer sbResult = new StringBuffer();
System.out.println(strInteger);
for (int i = 0; i < strInteger.length(); i++) {
sbResult.append(STR_MODIFY[i]);
sbResult.append(STR_NUMBER[strInteger.charAt(i) - 48]);
}


sbResult = sbResult.reverse();
replace(sbResult, "零十", "零");
replace(sbResult, "零百", "零");
replace(sbResult, "零千", "零");
replace(sbResult, "零万", "万");
replace(sbResult, "零亿", "亿");
replace(sbResult, "零零", "零");
replace(sbResult, "零零零", "零");
/** 这两句不能颠倒顺序 */
replace(sbResult, "零零零零万", "");
replace(sbResult, "零零零零", "");
/** 这样读起来更习惯. */
replace(sbResult, "壹十亿", "十亿");
replace(sbResult, "壹十万", "十万");
/** 删除个位上的零 */
if (sbResult.charAt(sbResult.length() - 1) == '零'
&& sbResult.length() != 1)
sbResult.deleteCharAt(sbResult.length() - 1);
if (strInteger.length() == 2) {
replace(sbResult, "壹十", "十");
}
try {
JspWriter JSPWriterOutput = pageContext.getOut();
JSPWriterOutput.print(sbResult);
} catch (Exception ioEx) {
System.out.println("IOException in HelloTag " + ioEx);
}
return (EVAL_PAGE);
}


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


private static void replace(StringBuffer pValue, String pSource,
String pDest) {
if (pValue == null || pSource == null || pDest == null)
return;
/** 记录pSource在pValue中的位置 */
int intPos = 0;
do {
intPos = pValue.toString().indexOf(pSource);
/** 没有找到pSource */
if (intPos == -1)
break;
pValue.delete(intPos, intPos + pSource.length());
pValue.insert(intPos, pDest);
} while (true);
}
}
3.在页面中实用。
<%@ taglib prefix="itags" uri="/itags" %>
<itags:NumTag num="${st.count}" />


具体介绍,参见struts2文档。
0 0