这个确实是有这个问题,所以在struts2里面,方法名,不要以get,set打头,否则他会以为是某个属性的getter() setter()方法。Ext经验当然也可以发上来分享啊

解决方案 »

  1.   

    前做项目的时候我有一个同事在一个action类里定义了一个get开头的方法,这个方法是action的默认方法调用的,但是他在测试的时候这个方法老是先执行,也就是说还没调用就自己执行了 ,因为自己执行一些参数没有传进来,所以就报错了,怎么测试也不对,也没发现问题.他就纠结了,来找我了. 我看了一下,调试了一下发现这个方法是在浏览器发出action请求之后,struts2的servlet拦截到aciton请求,
      

  2.   

    Ext想学,但一直没有时间,唉
      

  3.   

    仅仅是猜测而已,要看源码实现~//总控制器Dispatcher类中的部分代码片段
      private static ThreadLocal<Dispatcher> instance = new ThreadLocal();  public static Dispatcher getInstance()
      {
        return (Dispatcher)instance.get();
      }  public static void setInstance(Dispatcher instance)
      {
        instance.set(instance);    if (instance != null) {
          Container cont = instance.getContainer();
          if (cont != null)
            ObjectFactory.setObjectFactory((ObjectFactory)cont.getInstance(ObjectFactory.class));
          else
            LOG.warn("This dispatcher instance doesn't have a container, so the object factory won't be set.");
        }
        else {
          ObjectFactory.setObjectFactory(null);
        }
      }
    这只是其中的core包中的部分源码,其中Container和ObjectFactory类中是在xwork包中的,如想进一步了解的同学,可以自己去看它的源码~
      

  4.   

    extJs 学了半途而废了,很想学..
      

  5.   

    你用的是哪个版本的??我用get起名没错呀?不要误导大家啊。各位如果觉得有问题可以和我讨论:[email protected]这是我的一个action方法:
    public String getCodeStr() throws Exception {
    运行良好,没有出半点错误
      

  6.   

    我的是Struts2.1.8
    起不起get名对 Action的属性赋值没有影响。
    你的项目出现问题,和set方法有问题,命名有所冲突。
    Struts2的参数拦截器在注入值的时候,有两种机制,(调用ognl)
    查看ognl-2.7.3.jar中的OgnlRuntime 类中  public static final boolean hasGetProperty(OgnlContext context, Object target, Object oname)
        throws IntrospectionException, OgnlException
      {
        Class targetClass = target == null ? null : target.getClass();
        String name = oname.toString();    return (hasGetMethod(context, target, targetClass, name)) || (hasField(context, target, targetClass, name));
      }  public static final boolean hasSetProperty(OgnlContext context, Object target, Object oname)
        throws IntrospectionException, OgnlException
      {
        Class targetClass = target == null ? null : target.getClass();
        String name = oname.toString();    return (hasSetMethod(context, target, targetClass, name)) || (hasField(context, target, targetClass, name));
      }
    里面的
        return (hasSetMethod(context, target, targetClass, name)) || (hasField(context, target, targetClass, name));
      }
    所以也许是set方法的问题,不是get方法问题
      

  7.   

    所以也许是set方法的问题,不是get方法问题
      

  8.   

    嗯 也遇到过同样的问题 因为在action 中写了getXX处理一些数据,结果总是得不得想要的结果 
    那时郁闷了好长时间 
      

  9.   

    我的项目里有些“私有”的get方法,没出现问题,共有的我没试!~
      

  10.   

    恩 学习了 以后在action方法中命名不会用 get和set了防止出错。
      

  11.   

             public void getTest(){
    System.out.println("lalallalallalalalalal");
             }
             public void setTest(){
            System.out.println("11111111111111111111111111");
             }我在action里加入了以上方法,但却没有得到执行。你的意思是set、get开头的就相当于构造函数一样的执行没错吧?
      

  12.   

    他不是当作构造函数一样执行了,是当作一个属性的getter 和 setter方法执行了,struts2 是自动将属性的值放入到request中的。当你调用的Action方法执行完毕后struts会去执行每个Action里面的getter方法,只有这样才能将Action里面数据带入到jsp页面上去。为什么会这样呢?给点代码例子吧,我想struts的实现也和这个大同小异:
    clasz:是当前调用的Action的class值
    object:是当前Action的实例
     
    PropertyDescriptor[] propertyDescriptores = Introspector
    .getBeanInfo(clasz).getPropertyDescriptors();
    for (PropertyDescriptor propertyDescriptor : propertyDescriptores) {
    Method m = propertyDescriptor.getReadMethod();
    if (m == null) {
    continue;
    }
    Object ob = m.invoke(object);
    request.setAttribute(propertyDescriptor.getName(), ob);
    }如果strtus2 是先去查找Action的Field,而不是去查找 PropertyDescriptor ,就不会出现只有get方法没有属性的方法。问题就出现在这里...
      

  13.   

    现在已经改了,我现在就经常使用get开头...没有出现问题2.1.8
      

  14.   

    有注解可以让拦截器对这个get失效的!
      

  15.   

    学习了,在Action类中最好不要以get或者set为开头的方法,struts2会在客户端发生Action请求时,自动的执行属性的get、set方法,所以struts2可能会误以为是属性的get、set方法