📄 attributetag.java
字号:
package cn.com.juneng.system.common.taglib;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import cn.com.juneng.system.common.COMMON;
/**
* 显示存储在pageContext, request, session中的对象的属性值,并能够针对不同的属性类型显示相应的格式,如date, double等
*
* @author wanghaifeng
*
*/
public class AttributeTag extends TagSupport {
private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
private static NumberFormat nf = new DecimalFormat("0.00");
// 待显示对象,如果是string类型则是在存储某个属性中的属性key
private Object source;
// 显示对象的属性,如果空这显示对象toString
private String attr;
// 显示格式,对date和double有效,缺省如上设置;
private String format;
// 显示缺省值;
private String emptyValue = "";
// 最多显示长度
private int showLength;
public void setAttr(String attr) {
this.attr = attr;
}
public void setFormat(String format) {
this.format = format;
}
public void setSource(Object source) {
this.source = source;
}
public void setEmptyValue(String emptyValue) {
this.emptyValue = emptyValue;
}
public void setShowLength(int showLength) {
this.showLength = showLength;
}
private HttpServletRequest request() {
return (HttpServletRequest) super.pageContext.getRequest();
}
public int doStartTag() throws JspException {
Object obj = null;
if (source instanceof String) {// 一次在pageContext, request,
// session中搜索对象;
obj = super.getValue((String) source);
if (obj == null) {
obj = pageContext.getAttribute((String) source);
}
if (obj == null) {
obj = request().getAttribute((String) source);
}
if (obj == null) {
obj = request().getParameter((String) source);
}
if (obj == null) {
obj = request().getSession().getAttribute((String) source);
}
} else {
obj = source;
}
String displayValue = "";
if (obj != null) {
if (StringUtils.isEmpty(this.attr)) {
displayValue = obj.toString();
} else {
Object value = null;
if (obj instanceof Map) { // 对于map,attr中.号前表示map的key,.号后表示对象的属性;
Map map = (Map) obj;
if (!StringUtils.isEmpty(this.attr)) {
String mapAttr = "";
int index = this.attr.indexOf(".");
if (index == -1) {
mapAttr = attr;
attr = "";
} else {
mapAttr = this.attr.substring(0, index);
this.attr = this.attr.substring(index + 1);
}
obj = map.get(mapAttr);
}
}
try {
if (!StringUtils.isEmpty(this.attr)) {
if (this.attr.indexOf(".") < 0) {
value = PropertyUtils.getProperty(obj, attr);
} else {
value = PropertyUtils.getNestedProperty(obj, attr);
}
} else {
value = obj;
}
} catch (Exception e) {
//
}
// 支持数组类型,attr属性表示数组序号
if (obj != null && obj.getClass().isArray()) {
if (StringUtils.isEmpty(this.attr))
this.attr = "0";
try {
value = Array.get(obj, Integer.parseInt(this.attr));
} catch (Exception e) {
//
}
}
if (value != null) {
if (value instanceof BigDecimal) {
// BigDecimal -> Double
value = new Double(((BigDecimal) value).doubleValue());
}
if (value instanceof Date) { // 格式化日期
if (StringUtils.isEmpty(this.format)) {
if (value instanceof Timestamp) {
if(value.toString().indexOf("00:00:00")==-1){
df = new SimpleDateFormat(COMMON.TIME_FORMAT);
}else{
df = new SimpleDateFormat(COMMON.DATA_FORMAT);
}
}
displayValue = df.format((Date) value);
} else {
DateFormat df = new SimpleDateFormat(this.format);
try {
displayValue = df.format((Date) value);
} catch (Exception e) {
// ignore
}
}
} else if (value.getClass() == double.class
|| value.getClass() == Double.class
|| value.getClass() == float.class
|| value.getClass() == Float.class) {
// 格式化浮点数
if (StringUtils.isEmpty(this.format)) {
displayValue = nf.format(((Double) value)
.doubleValue());
} else {
double doubleValue = ((Double) value).doubleValue();
NumberFormat nf = new DecimalFormat(this.format);
try {
displayValue = nf.format(doubleValue);
} catch (Exception ignore) {
//
}
}
} else {
displayValue = value.toString();
}
}
}
}
if (COMMON.isEmpty(displayValue)) {
displayValue = this.emptyValue;
}
if (showLength != 0) {
displayValue = COMMON.showValue(displayValue, showLength);
}
JspWriter out = super.pageContext.getOut();
try {
out.print(displayValue);
} catch (Exception e) {
throw new JspException(e);
}
//
this.format = null;
this.attr = null;
this.emptyValue = "";
this.source = null;
//
return SKIP_BODY;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -