📄 priceformattag.java
字号:
package com.webshop.tag;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.Hashtable;
import java.io.Writer;
import java.io.IOException;
import java.util.Date;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.util.RequestUtils;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
* @author w
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
/**
*演示从TagSupport继承来开发标签
*/
public class PriceFormatTag extends TagSupport
{
protected String name;//Java Bean的名字
protected String property;//JavaBean的属性
protected String format;//要显示的数据的格式
/** 属性方法**/
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setFormat(String format) {
this.format = format;
}
public String getFormat() {
return format;
}
public void setProperty(String property) {
this.property = property;
}
public String getProperty() {
return property;
}
/**
*覆盖doStartTag方法
*/
public int doStartTag() throws JspTagException {
return EVAL_BODY_INCLUDE;
}
/**
*覆盖doEndTag方法
*/
public int doEndTag()throws JspTagException
{
try {
String beanName = Constants.BEAN_KEY;
if (name != null) {
beanName = name;
}
Object value = RequestUtils.lookup(pageContext, beanName, property, null);
if (value != null) {
String result;
NumberFormat formatter = new DecimalFormat(format);
result=formatter.format(value);
pageContext.getOut().write(result);
}
} catch (IllegalArgumentException e) {
// nested reference to a property returns null
} catch (Exception e) {
e.printStackTrace();
throw new JspTagException(e.getMessage());
}
return EVAL_PAGE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -