框架: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. python简明手册学习

    1.行末单独一个反斜杠表示字符串在下一行继续,而不是开始一个新的行. >>> "This is the first sentence.\ ... This is the s ...

  2. #define 中#和##的作用

    #的作用是把后面的参数变成一个字符串. 如,#define f(a) #a f(hello world)相当于"hello world": ##的作用是把两个字符串连接起来. 如, ...

  3. [AngularJS] 入门

    什么是AngularJS AngularJS是Google开源的一款JavaScript MVC框架,弥补了HTML在构建应用方面的不足, 其通过使用指令(directives)结构来扩展HTML词汇 ...

  4. 在Mac mini上编译Android源码

    参考文章 1.Android 6.0 源代码编译实践 2.编译Android源码致命错误解决方案 实践过程 1.Mac下安装Ubuntu双系统 (1)Ubuntu版本:Ubuntu 15.10 注:实 ...

  5. 纯CSS多级菜单

    主要代码部分: /*新增的二级菜单部分*/ .menu ul ul { visibility:hidden;/*开始时是隐藏的*/ position:absolute; left:0px; top:3 ...

  6. iOS企业级开发初级课程-表视图(13集)

    首先了解了表视图的组成.表视图类的构成.表视图的分类以及表视图的两个重要协议(委托协议和数据源协议),对表视图有了一个整体上的认识.接下来我们掌握了如何实现简单表视图和分节表视图,以及表视图中索引.搜 ...

  7. 极客DIY:如何用Siri与树莓派“交互”

    苹果在2014年推出的HomeKit智能家居平台的确给人眼前一亮的感觉.随着时间的推移,国外的黑客对HomeKit该逆向的逆向,结果也都汇总到了git.本着折腾到死的极客心态,从网上淘了一块树莓派进行 ...

  8. NOIP2015 斗地主

    Sol 暴力搜索...不用搜1,2张的情况,直接统计出来就可以了. 还有处理一下2和大王小王的位置,扔到最后面或者最前面就可以了. 搜索就搜 3+3,2+2+2,1+1+1+1+1 这三个就可以了. ...

  9. zencart安装后修改configure.php权限

    刚安装完zencart的时候,打开前台页面会遇到下面的安全提示: “警告: 配置文件允许写入: E:\upupw\htdocs\mysite\includes\configure.php,存在安全隐患 ...

  10. PHP--获取响应头(Response Header)方法

    方法一: $baiduUrl = "http://www.baidu.com/link";   file_get_contents($baiduUrl); $responseInf ...