在Action初始化的时间引入ActionValues,并把ActionValues继承HashMap,ActionValues如果传递为对象会自动通过mysql拿到当前对象实体名称,然后根据实体的名称来当Key封装value值,如果没有参数默认是传递request,然后自动解析字符串存入map,并会过滤spring等一些配制文件的参数数据。
/**
* ActionValues数据封装
* @author zhouguohui
*/
public class ActionValues extends HashMap<String,Object>{
public ActionValues(){
super();
}
public ActionValues(Object obj){
String[] fieldNames=ModelUtils.getFieldNames(obj.getClass());
try{
for(String fieldName:fieldNames){
this.put(fieldName,BeanUtils.getProperty(obj, fieldName)==null?"":BeanUtils.getProperty(obj, fieldName));
}
}catch(Exception e){
e.printStackTrace();
}
}
public ActionValues(Map<String,Object> values){
this.putAll(values);
}
@SuppressWarnings("unchecked")
public ActionValues(HttpServletRequest request){
String name=null;
Enumeration<String> paramNames=request.getParameterNames();
Enumeration<String> attrNames=request.getAttributeNames();
while(paramNames.hasMoreElements()){
name=paramNames.nextElement();
if(name.startsWith("org.springframework")||name.equals("characterEncodingFilter.FILTERED")||name.equals("shiroFilter.FILTERED")||name.equals("roles.FILTERED")){//过滤spring参数
continue;
}
if(request.getParameterValues(name).length>1){
String[] values=request.getParameterValues(name);
for(String v:values){
if(StrUtils.isNotEmpty(v)){
put(name,v);
}
}
}else{
if(null!=request.getParameter(name)&&StrUtils.isNotEmpty(String.valueOf(request.getParameter(name)))){
put(name, request.getParameter(name));
}
}
}
while(attrNames.hasMoreElements()){
name=attrNames.nextElement();
if(name.startsWith("org.springframework")||name.equals("characterEncodingFilter.FILTERED")||name.equals("shiroFilter.FILTERED")||name.equals("roles.FILTERED")){//过滤spring参数
continue;
}
if(null!=request.getAttribute(name)){
put(name,request.getAttribute(name));
}
}
}
@Deprecated
public void addValues(Object obj){
String[] fieldNames=ModelUtils.getFieldNames(obj.getClass());
try{
for(String fieldName:fieldNames){
if(null!=BeanUtils.getProperty(obj, fieldName)&&!"".equals(BeanUtils.getProperty(obj, fieldName).trim())){
this.put(fieldName,BeanUtils.getProperty(obj, fieldName));
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}