struts2.0整合json

      框架:struts2.0+hibernate2+spring

     今天写代码时,需要用到json,我就直接加了两个jar包:json-lib-2.1-jdk15.jar,struts2-json-plugin-2.2.3.jar。启动不起来。我用的是struts-core-2.0.11.jar,很明显不兼容报错。后来我在网上找到可以用jsonplugin-0.32.jar。json插件在http://code.google.com/p/jsonplugin/downloads/list下载,注意版本。struts2.1以上版本要下载jsonplugin-0.34.jar否则会有问题。

     网上说定义的package必须继承json-default。但当按"alt+/"时没有提示json-default,因为json-default中的代码是:

    <package name="json-default" extends="struts-default">
        <result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
        </result-types>
        <interceptors>
            <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
        </interceptors>
    </package>

所以我直接把:

        <result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
        </result-types>
        <interceptors>
            <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
        </interceptors>

放在自己定义的package下如:

     <package name="purchases" extends="base-action" namespace="/purchases">

       <result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
        </result-types>
        <interceptors>
            <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
        </interceptors>

     <action name="addPurchasesInfo" class="com.wisdom.lxgz.purchases.action.PurchasesAction"
             method="addPurchasesInfo">
        <interceptor-ref name="ebStackWithTokenAndValidation" />
        <interceptor-ref name="chain" />
        <result type="json"></result>
        <result name="input">/jsp/purchases/initPurchasesInfo.jsp</result>
        <result name="invalid.token" type="chain">purchasesList</result>
       <exception-mapping result="success" exception="java.lang.Exception" />
    </action>

   </package>

启动程序时报错:java.lang.ClassNotFoundException: org.apache.struts2.json.JSONResult.

我把struts2-json-plugin-2.2.3.jar解压后发现是有org.apache.struts2.json.JSONResult,但当解压jsonplugin-0.32.jar后发现JSONResult这个class是在com.googlecode.jsonplugin.JSONResult下,所以上面的路径改成com.googlecode.jsonplugin.JSONResult。

改好后启动没有报错,但当action向前台传值时报错:

java.lang.IllegalAccessException: Class com.googlecode.jsonplugin.JSONWriter can not access a member of class org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper with modifiers "public"
 at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
 at java.lang.reflect.Method.invoke(Method.java:588)
 at com.googlecode.jsonplugin.JSONWriter.bean(JSONWriter.java:224)
 …………

网上有人说是因为有类型不能序列化,所以爆出此错,网上说的四个解决方法: 

1.在get Service方法上面加

      @JSON(serialize = false)

      例如:

     @JSON(serialize = false)
     public SuperviseService getSuperviseService() {
         return safetySuperviseService;
     }
    2、修改JSONWriter 
    3、增加transient 
    4、去掉get方法

我按照方法1,在action类中把service对象的get()方法上加上@JSON(serialize=false)后,果然不报错了,并且在jsp上可以得到值了。

 

转载于:https://www.cnblogs.com/gexiaoshan/p/3404550.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值