⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 baseform.java

📁 这是一个轻便的j2ee的web应用框架,是一个在多个项目中运用的实际框架,采用struts,hebinate,xml等技术,有丰富的tag,role,navigation,session,dictio
💻 JAVA
字号:
package com.esimple.framework.web.action;

import java.util.*;
import java.math.BigDecimal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.validator.*;
import com.esimple.framework.util.*;

/**
 * Map-backed actinForm
 * @author steven
 * @version 0.9
 */
public class BaseForm extends ValidatorActionForm {

	private final Map values = new HashMap();
	protected Log logger = LogFactory.getLog(this.getClass());
	
	/**
	 * 获得Map
	 * @param key
	 * @return Map
	 */
	public Map getValues() {
		return values;
	}
	
	/**
	 * 读取map中的指定值
	 * @param key
	 * @return
	 */
	public Object getValue(String key) {
		return values.get(key);
	}	
	
	/**
	 * 读取map中的指定值
	 * @param key
	 * @return
	 */
	public String getValueAsString (String key) {
		Object obj = values.get(key);
		if( obj == null ) return null;
		return obj.toString();
	}
	
	/**
	 * 读取map中的指定值,返回字符串
	 * 如果trim设置为true,字符串将会进行去空格处理,如果去空格后字符串为空
	 * 则返回null;
	 * @param key
	 * @param trim 是否去空格
	 * @return
	 */
	public String getValueAsString (String key,boolean trim) {
		Object obj = values.get(key);
		if( obj == null ) return null;
		if( trim ){
			String temp = (obj.toString()).trim();
			if( temp.length() ==0  ) return null;
			return temp;
		}else{
			return obj.toString();
		}
	}
	
	/**
	 * 设置map中的值
	 * @param key
	 * @param value
	 */
	public void setValue(String key, Object value) {
		if(value instanceof String ) 
			values.put(key, StringUtils.native2unicode((String)value));
		else
			values.put(key, value);
	}	
	/**
	 * 设置map中的值
	 * @param key
	 * @param value
	 */	
	public void setValue(String key, int value) {
		values.put(key, value+"");
	}
	
	/**
	 * 设置map中的值
	 * @param key
	 * @param value
	 */	
	public void setValue(String key, float value) {
		values.put(key, value+"");
	}	
	
	/**
	 * 设置map中的值
	 * @param key
	 * @param double
	 */
	public void setValue(String key, double value) {
		values.put(key, value+"");
	}	
	
	/**
	 * 读取map中的值并转换为int
	 * @param key
	 * @return int
	 * @throws ConvertException
	 */
	public int getValueAsInt(String key) throws ConvertException {
		if ( key == null ) throw new ConvertException(
			"can not getValue because the field name is null");
		String strValue = getValueAsString( key,true );
		if ( strValue == null ) throw new ConvertException(
					"can not convert value to int because the field value is null");
		return DataTypeConverter.toInt( strValue  );
	}
	
	/**
	 * 读取map中的值并转换为float
	 * @param key
	 * @return float
	 * @throws ConvertException
	 */
	public float getValueAsFloat(String key) throws ConvertException {
		if ( key == null ) throw new ConvertException(
			"can not getValue because the field name is null");
		String strValue =  getValueAsString( key,true );
		if ( strValue == null ) throw new ConvertException(
					"can not convert value to float because the field value is null");
		return DataTypeConverter.toFloat( strValue  );
	}

	/**
	 * 读取map中的值并转换为double
	 * @param key
	 * @return double
	 * @throws ConvertException
	 */
	public double getValueAsDouble(String key) throws ConvertException {
		if ( key == null ) throw new ConvertException(
			"can not getValue because the field name is null");
		String strValue =  getValueAsString( key,true );
		if ( strValue == null ) throw new ConvertException(
					"can not convert value to double because the field value is null");
		return DataTypeConverter.toDouble( strValue  );
	}	
	
	public String[] getValueAsArray(String key) throws ConvertException {
		if ( key == null ) throw new ConvertException(
			"can not getValue because the field name is null");
		String[] strValue;
		try{
			strValue =(String[]) getValue( key );
		}catch(Exception e){
			strValue =new String[1];
			strValue[0] = getValue( key ).toString();
		}
		
		
		if ( strValue == null ) throw new ConvertException(
					"can not convert value to double because the field value is null");
		return strValue  ;
	}	
	
	/**
	 * 读取map中的值并转换为BigDecimal
	 * @param key
	 * @return int
	 * @throws ConvertException
	 */
	public BigDecimal getValueAsBigDecimal(String key) throws ConvertException {
		if ( key == null ) throw new ConvertException(
			"can not getValue because the field name is null");
		String strValue =  getValueAsString( key,true );
		if ( strValue == null ) throw new ConvertException(
					"can not convert value to int because the field value is null");
		return DataTypeConverter.toBigDecimal( strValue  );
	}
	/**
	 * 打印map中的数值,必须将log级别设为debug
	 * @see org.apache.log4j
	 */
	public void printValues(){
		if( values.isEmpty()) {
			logger.info("printValues in BaseForm:" + " values is null");
		}else{
			Iterator keys = ((Set) values.keySet()).iterator();
			logger.info( "****contents in BaseForm"+ this.toString()+"****");
			while ( keys.hasNext() ){
				String key = keys.next().toString();
				 if( values.get(key) instanceof String[] ){
					logger.info("   key="+key +";value length="+ ((String[])values.get(key)).length);
				 }else{
					logger.info("   key="+key +";value="+ values.get(key));
				 }
				
			}
			logger.info( "****          end          **** ");
			
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -