?? pagedataimpl.java
字號(hào):
package com.onetsoft.fastjsp;
import com.onetsoft.fastjsp.request.PageData;
import com.onetsoft.fastjsp.request.UploadFile;
import com.onetsoft.fastjsp.request.multipart.MultipartDecoder;
import com.onetsoft.fastjsp.util.StringUtils;
import com.onetsoft.fastjsp.util.ValueParser;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* @author <a href="mailto:hgw@onetsoft.com">hgw</a>
*/
class PageDataImpl implements PageData {
private MultipartDecoder decoder = null;
private Map map = null;
private AbstractServicer servicer = null;
public PageDataImpl(AbstractServicer servicer, MultipartDecoder decoder) {
this.servicer = servicer;
this.map = servicer.pageParams.parameterMap;
this.decoder = decoder;
parseAction();
parseSubmitMode();
}
/**
* 解析url or form 操作參數(shù)
*/
private void parseAction() {
boolean actionParsed = false;
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
Map.Entry e = (Map.Entry) iter.next();
String paraName = e.getKey().toString();
if (isActionParam(paraName)) {
int index = paraName.indexOf('_');
//form action
if (index != -1) {
map.put(servicer.module.actionName,(paraName.length()-1)>index?paraName.substring(index + 1).trim():StringUtils.EMPTY);
map.remove(paraName);
}
//url action
else {
e.setValue(e.getValue() == null ? StringUtils.EMPTY : e.getValue().toString().trim());
}
actionParsed = true;
break;
}
}
if (!actionParsed)
map.put(servicer.module.actionName, StringUtils.EMPTY);
}
/**
* 解析表單是否"cancel"
*/
private void parseSubmitMode() {
String form = getValue(StringUtils.FORM);
if (form == null) return;
if (form.endsWith(StringUtils.CANCEL_FORM_TAG)) {
map.put(StringUtils.FORM, form.substring(0, form.indexOf(StringUtils.CANCEL_FORM_TAG)));
map.put(StringUtils.CANCEL_FORM_TAG, StringUtils.EMPTY);
}
}
private boolean isActionParam(String paramName) {
return servicer.module.actionName.equalsIgnoreCase(paramName) ||
(paramName.indexOf(servicer.module.actionName)==0&¶mName.length()>servicer.module.actionName.length()&&
paramName.charAt(servicer.module.actionName.length())=='_');
}
public String getAction() {
return map.get(servicer.module.actionName).toString();
}
public Map getMap() {
return map;
}
public void put(Map map) { Map.Entry e;
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
e = (Map.Entry) iter.next();
if (e.getValue() != null)
this.map.put(e.getKey(), e.getValue());
}
}
public Set keySet() {
return map.keySet();
}
public void put(Object name, Object value) {
if (name == null)
throw new NullPointerException("Param \"name\" cannot be null!");
if (value != null)
map.put(name, value);
}
public void put(Object bean) {
try {
DataSqueezer squeezer = servicer.getModule().getDataSqueezerFactory().getDataSqueezer();
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor descriptors[] = beanInfo.getPropertyDescriptors();
Object propertyValue;
for (int i = 0; i < descriptors.length; i++) {
try {
propertyValue = descriptors[i].getReadMethod().invoke(bean, null);
map.put(descriptors[i].getName(), squeezer.squeeze(propertyValue));
} catch (Exception e) {
/*ingored*/
}
}
} catch (IntrospectionException e) {
throw new ApplicationRuntimeException(e);
}
}
public Object get(Object name) {
return map.get(name);
}
public Object get(Class beanClass) {
Object bean = null;
try {
DataSqueezer squeezer = servicer.getModule().getDataSqueezerFactory().getDataSqueezer();
bean = beanClass.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
PropertyDescriptor descriptors[] = beanInfo.getPropertyDescriptors();
Object propertyValue;
for (int i = 0; i < descriptors.length; i++) {
try {
propertyValue = squeezer.unsqueeze(descriptors[i].getPropertyType(), (String) map.get(descriptors[i].getName()));
descriptors[i].getWriteMethod().invoke(bean, new Object[]{propertyValue});
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (InstantiationException e) {
throw new ApplicationRuntimeException(e);
} catch (IllegalAccessException e) {
throw new ApplicationRuntimeException(e);
} catch (IntrospectionException e) {
throw new ApplicationRuntimeException(e);
}
return bean;
}
public void remove(Object name) {
map.remove(name);
}
public void clear() {
map.clear();
}
public UploadFile getFileUpload(String parameterName) {
return decoder.getFileUpload(parameterName);
}
public UploadFile[] getFileUploads() {
return decoder.getFileUploads();
}
public String[] getParameterValues(String name) {
return ParamsHelper.getParameterValues(map, name);
}
/**
* 若取得值為數(shù)組,則返回第一個(gè)值
* 此方法主要針對(duì)傳入的參數(shù)而言
*
* @param name
* @return
*/
private String getValue(String name) {
return ParamsHelper.getParameter(map,name);
}
public String getString(String name, String defaultValue) {
String s = getValue(name);
return s == null ? defaultValue : s;
}
public String getString(String name) {
return getString(name, StringUtils.EMPTY);
}
public void setString(String name, String value) {
put(name, value);
}
public boolean getBoolean(String name, boolean defaultValue) {
return ValueParser.parseBoolean(getValue(name), defaultValue);
}
public boolean getBoolean(String name) {
return getBoolean(name, false);
}
public void setBoolean(String name, boolean value) {
put(name, value ? "1" : "0");
}
public double getDouble(String name, double defaultValue) {
return ValueParser.parseDouble(getValue(name), defaultValue);
}
public double getDouble(String name) {
return getDouble(name, 0D);
}
public void setDouble(String name, double value) {
put(name, Double.toString(value));
}
public long getLong(String name, long defaultValue) {
return ValueParser.parseLong(getValue(name), defaultValue);
}
public void setLong(String name, long value) {
put(name, Long.toString(value));
}
public long getLong(String name) {
return getLong(name, -1L);
}
public int getInt(String name, int defaultValue) {
return ValueParser.parseInt(getValue(name), defaultValue);
}
public int getInt(String name) {
return getInt(name, 0);
}
public void setInt(String name, int value) {
put(name, Integer.toString(value));
}
public String toString() {
return map.toString();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -