关于struts2 中action 属性没有 set 方法 依然会注入值的问题(解决)

    xiaoxiao2021-03-25  317

    以前经常看到网上和书上说,struts2 action中的属性中要写相对应的setXXx,getXXx方法才能接收到网页中传来的参数! 无意中发现,属性没有写set get 方法,依然会注入值和取到值. 甚是不解,求助于网上依然没有得到解决! 花了点时间看了下源码! 发现: struts2 参数拦载器在注入值的时候,有两种机制,(调用ognl)   这是OgnlRuntime 类中一段源码 hasSetMethod( context, target, targetClass, name ) || hasField( context, target, targetClass, name );  

    他会先查看你的action 中有没有对应的方法,如果发现无此方法,他会利用第二种方法直接给属性覆值,而无需对应的set方法!(当然都是通过反射机制实现的)

    from:

    东avaj东的博客 http://blog.sina.com.cn/u/1866397122 

    转载请注明原文地址: https://ju.6miu.com/read-17.html

    最新回复(0)