struts和ajax结合报错

图片说明
上网查了关于Positioned Update not supported.都是因为action中有多余的get方法,导致被搞成了json数据格式,但是我的代码里没有多余的get方法,所以不是网上哪种常见的错误。求大神
下面是我的struts配置文件
图片说明

这是我的js脚本
$.post("menuitemAjax_showMenuitem.action",null,function(data){
alert(data);
privilege.pFunction.privilegeTree.zTree = $("#privilegeTree").zTree(privilege.pFunction.privilegeTree.setting, data.menuitemList);

    报错信息如下
    Struts Problem Report

Struts has detected an unhandled exception:
Messages:

Positioned Update not supported.
java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException
org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: org.apache.struts2.json.JSONException: java.lang.reflect.InvocationTargetException

File: com/mysql/jdbc/SQLError.java
Line number: 1,055

那么,为什么会出现这个异常呢?

究其原因,竟然是Hibernate的懒加载引起的。一定是你传递的数据中有引用类型的数据采用了懒加载机制。

比如:我要从Action中向前台传送一个Collection,通过get方法

public Collection getMenuitemList() {
return this.menuitemList;
}

而其中Menuitem类型的数据结构是:

public class Menuitem {
private Long mid;
private Long pid;//父节点
private String name;//树的节点的名称
private Boolean isParent;//是否为父节点
private String icon;//图标的路径
private Boolean checked;//复选框是否被选中
/**

  • 菜单权限与用户是多对多的关系 */ private Set users;

//getter(),setter()方法

}

在映射文件Menuitem.hbm.xml中users属性的配置如下:




未设置lazy="false",则默认采用懒加载模式。

当中间表user_menuitem中没有数据时,ok,不会牵涉到use表,也就不存在懒加载问题,运行一切正常。

但是当user_menuitem表中有数据,且数据与所传送的Menuitem对象有关时,就会有问题了

因为懒加载,这样在Action中获取的Menuitem对象中的users属性中的set集合中会存在user对象的引用,但是user的信息其实并未加载,

只有当用到时,容器才再次发出sql请求进行加载,但是在json插件对menuitemList进行处理以转换成json格式时,Hibernate Session早已关闭,这样user信息便加载不成功,而它又无法对set集合中空的引用进行处理,所以便抛出了JSONException。

有人可能会有疑问,我在web.xml中配置了OpenSessionInViewFilter过滤器,为什么Session还会关闭呢!?

这个疑问问的好!!我也不知何故,在网上也没找到相关的解释,不知是不是ajax的XMLHttpRequest的问题,

总之,在struts2与ajax结合的过程中,OpenSessionInView模式不起作用了!请知道的朋友不吝赐教!

好!既然知道了问题的原因,那么解决方法就很明了了!

方法一:设置lazy="false",即对user不采用懒加载。如

     <set name="users" table="user_menuitem" inverse="true"lazy="false">
     <key column="mid"></key>
     <many-to-many column="uid" class="cn.myoa.domain.User"></many-to-many>
     </set>

      不过这时要注意在User对象中有没有其他对象的引用,用过有,也要设置为非懒加载模式。

方法二:忽略set users 属性,(推荐使用)

      如果在前台页面不需要使用该属性的话,就不要把他传到前台去,设置方法是在其getter方法上加一注解: @JSON(serialize=false)

      @JSON(serialize=false)
      public Set<User> getUsers() {
              return users;
      }

     这样json插件在转换数据时就会忽略该属性。

现在问题应该已经解决了!!

下面关于struts2和ajax的结合还有几点建议:

1、在页面用不到的数据最好不要传到前台(这也是之所以推荐第二种方法的原因,传的数据越大,效率越低不是吗!)

2、不是向前台传数据的方法最好不要以get开头,json插件会把所有get开头的方法当做属性,转为json格式数据

3、如果方法必须以get开头,然而又不是为了转为json格式,那么可以在该方法上加注解:@JSON(serialize=false)

4、需要传到前台的数据,一定要在dao中加载完毕,不能使用懒加载模式。

我自己解决了,要吐槽一下百度的搜索功能真是他的差劲!

http://bbs.csdn.net/topics/360161158