框架: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上可以得到值了。

struts2.0整合json的更多相关文章

  1. struts2整合json要注意的问题

    昨天struts2整合json,一直出错: There is no Action mapped for namespace / and action name ... HTTP Status 404 ...

  2. Struts2.0笔记二

    Mvc与servlet 1.1   Servlet的优点 1.  是mvc的基础,其他的框架比如struts1,struts2,webwork都是从servlet基础上发展过来的.所以掌握servle ...

  3. struts2+hibernate整合开发步骤

    百度的各种代码,步骤,自己整合了一下 1,创建数据库 常用mysql   creat table..... 2,在WebContent下的bin中添加相应的包 http://pan.baidu.com ...

  4. (转)struts2.0配置文件、常量配置详解

    一.配置: 在struts2中配置常量的方式有三种: 在struts.xml文件中配置 在web.xml文件中配置 在sturts.propreties文件中配置 1.之所以使用struts.prop ...

  5. struts2中的json

    这里放一个转载的struts2中json的详细应用和范例, http://yshjava.iteye.com/blog/1333104,这是个人在网上看到的很用心也很详细的一份关于struts2中js ...

  6. 玩转spring MVC(八)----spring MVC整合json

    这篇文章在前边项目的基础上来整合json,最新项目资料见:http://download.csdn.net/detail/u012116457/8436187 首先需要的jar包:jackson-co ...

  7. 项目ITP(六) spring4.0 整合 Quartz 实现动态任务调度

    前言 系列文章:[传送门] 项目需求: http://www.cnblogs.com/Alandre/p/3733249.html 上一博客写的是基本调度,后来这只能用于,像每天定个时间 进行数据库备 ...

  8. 项目ITP(五) spring4.0 整合 Quartz 实现任务调度

    前言 系列文章:[传送门] 项目需求: 二维码推送到一体机上,给学生签到扫描用.然后需要的是 上课前20分钟 ,幸好在帮带我的学长做 p2p 的时候,接触过.自然 quartz 是首选.所以我就配置了 ...

  9. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口(转)

    转自:[CXF REST标准实战系列] 二.Spring4.0 整合 CXF3.0,实现测试接口 文章Points: 1.介绍RESTful架构风格 2.Spring配置CXF 3.三层初设计,实现W ...

随机推荐

  1. 关于Html编码问题,例如字符:&#183;

    我写的WCF服务突然报错了... 然后我发现传过来的字符不完整 {"完整":"尼古拉·奥斯特洛夫斯基的信息"} 然后传过来的是:{"完整": ...

  2. SQL Server 服务器器信息备份(一)--login新建脚本备份

    前言 若你的企业使用SQL Server数据库镜像为容灾技术. 那你一定做过在镜像切换之前要新建Login,而且若Login密码不同,要修改链接数据库的字符串,在切换完之后则仍需要给数据库重新赋予权限 ...

  3. Ruby学习之mixin

    直接上代码: module Action def jump @distance = rand(4) + 2 puts "I jumped forward #{@distance} feet! ...

  4. 2013区域赛长沙赛区现场赛 K - Pocket Cube

    K - Pocket Cube Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  5. 真机调试 —— An unknown error occurred.

    iOS开发中,总会遇见各种各样的问题.今天我就在真机调试的时候出现 An unknown error occurred. 不知道什么鬼,百度一下,各种胡说八道. 解决办法: 1.退出Xcode,重新运 ...

  6. hadoop之根据Rowkey从HBase中查询数据

    1.Hbase 根据rowkey 查询 conf的配置信息如下: conf = new Configuration(); conf.set("hbase.zookeeper.quorum&q ...

  7. linux 之常见的好用命令

    参考网址:软件匠艺小组之第八期把命令行玩飞起来 1.如果想要将文件重定向到文件里,而又想看重定向的内容, tee命令 例如:ls | tee foot.txt 2.如果想要字母显示为大写独特的,命令: ...

  8. JAVA序列化的作用

    所谓的Serializable,就是java提供的通用数据保存和读取的接口.至于从什么地方读出来和保存到哪里去都被隐藏在函数参数的背后了.这样子,任何类型只要实现了Serializable接口,就可以 ...

  9. BZOJ2599——[IOI2011]Race

    0.题意:给一棵树,每条边有权.求一条路径,权值和等于K,且边的数量最小. 1.分析:水题一道,一波树分治就好 我们可以发现这个题的K是比较小的,才100w,那么我们可以树分治一下,在遍历每一棵子树的 ...

  10. 实战CentOS系统部署Hadoop集群服务

    导读 Hadoop是一个由Apache基金会所开发的分布式系统基础架构,Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS.HDFS有高 ...