📄 ex6_34.txt
字号:
Example 6.34 Auto-populating ContextObject: AutoPopulateRequestContext
// Context Object helper
public class AutoPopulateRequestContext {
public static void populateBean(Object bean, ServletRequest request) {
Enumeration enum = request.getParameterNames();
while ( enum.hasMoreElements()) {
String parameterName = (String)enum.nextElement();
if ( PropertyUtils.isWriteable(bean, parameterName)) {
String values[] = request.getParameterValues(parameterName);
try {
if ( values.length == 1 ) {
PropertyUtils.setSimpleProperty(
bean, parameterName, values[0]);
} else {
for ( int iValue=0; iValue < values.length; iValue++)
PropertyUtils.setIndexedProperty(
bean, parameterName, iValue, values[iValue]);
}
} catch (IllegalAccessException e) {
// handle exception
} catch (InvocationTargetException e) {
// handle exception
} catch (NoSuchMethodException e) {
// handle exception
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -