lundi 20 mars 2017

I want to use java reflection to access an object's attrs and values which is automatically filled by Struts2.x in my Action

I want to use java reflection to access an object's attrs and values which is automatically filled by Struts2.x in my Action,but the result is not as I expected,what's wrong?

Here's my Action:

@Controller("custAction")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class CustAction extends BaseAction

This is where the method calls:

public String  saveOrUpdate() throws Exception{

    String ret = Constants.STATUS_CUCCESS_JSON;
    try {
        CommonUtil.checkAttr(cust,null,null);

('cust' is a JavaBean)

here's 'cust' value:

'cust' value

log out:

log out

here's my method:

public static void checkAttr(Object vo,String errorMsg,HashMap<String, String> extra) throws IllegalInputException{

    Class<? extends Object> voClass = vo.getClass();
    Field[] field=voClass.getDeclaredFields();
    if(errorMsg==null){
        errorMsg="你的输入包含非法字符,请重新输入!";
    }
    for(Field attr: field){
        attr.setAccessible(true);
        Object value=null;
        try {
            value=attr.get(vo);//获取属性值!!
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("name:"+attr.getName()+"         value:"+value);
        //校验是否有要被区别对待的属性
        /*if(extra!=null&&extra.get(attr.getName())!=null){
            if(value!=null&&checkRegExp(value.toString(),extra.get(attr.getName()))){
                System.out.println("name:"+attr.getName()+"         value:"+value.toString());
                throw new IllegalInputException(errorMsg);
            }
        }else{
            if(value!=null&&checkRegExp(value.toString(),REGEX_INPUT)){
                System.out.println("name:"+attr.getName()+"         value:"+value.toString());
                throw new IllegalInputException(errorMsg);
            }
        }*/
    }

}

Why?Is that Spring cause this problem?





Aucun commentaire:

Enregistrer un commentaire