java - struts2 拦截器内 invoke 方法返回空
大家讲道理
大家讲道理 2017-04-17 15:06:18
0
1
352

我的 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,这是为什么?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(1)
伊谢尔伦

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;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!