框架: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. [译]git checkout

    git checkout git checkout提供3种不同的功能: checking out文件, checking out commits, checking out branch. check ...

  2. SQL 语句-partition by

    /****** ******/ 初始化数据 create table employee (empid int ,deptid int ,salary decimal(10,2)) insert int ...

  3. Eclipse经常报Unhandled event loop exception的原因

    在公司的电脑上,Eclipse经常报Unhandled event loop exception 错误,非常频繁,通过搜索发现是因为电脑上安装了百度杀毒导致的.... 无语 另外 teamviewer ...

  4. Linux 信号量大全

    编号 信号名称 缺省动作 说明 1 SIGHUP 终止 终止控制终端或进程 2 SIGINT 终止 键盘产生的中断(Ctrl-C) 3 SIGQUIT dump 键盘产生的退出 4 SIGILL du ...

  5. webrtc第二篇 聊天室

    聊天室模型不一样考虑的问题也不一样 1.websocket文本聊天 * step1 : 向聊天室所有用户(不包括该用户自己)发送当前用户上线信息.客户端用户栏回添加此用户 * step2 : 将该用户 ...

  6. linux远程复制和压缩文件的命令

    1.linux导出指定数据库表 mysqldump  127.0.0.1  -uname -ppwd  databasename  tablename>/home/name/tablename. ...

  7. 如何开启telnet 23端口

    netstat -tnl|grep 23 查看23端口是否开启 或者 chkconfig --list|grep telnet 检查telnet状态 如果关闭状态, 开启:chkconfig --le ...

  8. Unity 用户手册用户指南二维纹理 (Texture 2D)

    http://www.58player.com/blog-2327-953.html 二维纹理 (Texture 2D) 纹理 (Textures) 使您的 网格 (Meshes).粒子 (Parti ...

  9. hustoj1353 节点选择 树形dp

    1353: 结点选择 时间限制: 1 Sec  内存限制: 128 MB提交: 6  解决: 2[提交][状态][讨论版] 题目描述 问题描述 有一棵 n 个节点的树,树上每个节点都有一个正整数权值. ...

  10. openerp child_of操作符深度解析

    child_of  此操作符,从代码来看,等价于: [('x','child_of',id)] ==>  x.prarent_left >=id.parent_left && ...