search

Home  >  Q&A  >  body text

java - struts2 拦截器内 invoke 方法返回空

我的 struts2 拦截器内有以下代码

@Override
public String intercept(ActionInvocation ai) throws Exception {
    Object object = ai.getAction();
    Class<? extends Object> clazz = ai.getAction().getClass();
    Field[] fields = clazz.getDeclaredFields();

    try {
        for(Field field : fields) {
            PropertyDescriptor pd = null;
            try {
                pd = new PropertyDescriptor(field.getName(), clazz);  
            } catch (Exception e) {
                continue;
            }
                
            Method getMethod = pd.getReadMethod();     
            Method setMethod = pd.getWriteMethod();    
                
            Object o = getMethod.invoke(object); 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}    

但是这句 Object o = getMethod.invoke(object); 里的 o 一直都是 null,这是为什么?

大家讲道理大家讲道理2770 days ago392

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:08:18

    Is the target field you want to obtain obtained after the action is executed in program logic, or can it be obtained only if there is an action object?

    If it is pre-positioned, you need to complete the execution of the call chain in the interceptor:

    String result = ai.invoke();
    Object object = ai.getAction();
    Class<? extends Object> clazz = ai.getAction().getClass();
    Field[] fields = clazz.getDeclaredFields();
    
    try {
        for(Field field : fields) {
            PropertyDescriptor pd = null;
            try {
                pd = new PropertyDescriptor(field.getName(), clazz);  
            } catch (Exception e) {
                continue;
            }
                
            Method getMethod = pd.getReadMethod();     
            Method setMethod = pd.getWriteMethod();    
                
            Object o = getMethod.invoke(object); 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;

    reply
    0
  • Cancelreply