struts2 与json整合无法 识别 type="json"问题

简介: 引用:http://www.cnblogs.com/ini_always/archive/2011/10/15/2213404.html   struts2的json plugin可以实现struts2和json的完美结合,由于本篇主要是介绍整合过程中遇到的问题,所以编程实现的方法这里就不重复了,具体可以参看struts2的官方文档:http://struts.apache.org/2.2.1.1/docs/json-plugin.html。

引用:http://www.cnblogs.com/ini_always/archive/2011/10/15/2213404.html

 

struts2的json plugin可以实现struts2和json的完美结合,由于本篇主要是介绍整合过程中遇到的问题,所以编程实现的方法这里就不重复了,具体可以参看struts2的官方文档:http://struts.apache.org/2.2.1.1/docs/json-plugin.html

我在struts.xml中有如下action定义:

<action name="product_group" class="customers.products" method="getGroups">
<result type="json">
<param name="root">groupList</param>
</result>
</action>

在上面的定义中,action的result的type为json,json plugin就可将action中定义为groupList的field自动转换为json格式数据,并返回给前端UI。

但在deploy后,启动tomcat时却报了There is no result type defined for type 'json' mapped with name 'success'. Did you mean 'json'?的错误,因为struts2找不到json这个result type的定义。解决方法有下面两种:

 

1.将当前package的extends属性改为"json-default",即让当前package从josn-default继承而不是struts-default继承;

 

2.但如果当前package确实无法继承"json-default"的话,还可以在当前package中定义result-type,将json给加进去,如下:

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

两种方法的原理:

json这个result type是在json-default  (struts2-json-plugin-2.1.8.1.jar\struts-plugin.xml)里面定义的,内容如下(省去了xml和doctype标签):

复制代码
<struts>
<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>
</struts>
复制代码

可见,name为"json"的result type是在json-default中定义的,所以,从json-default继承就可以使用json这个result。另外json-default还定义了一个name为"json"的interceptor。

另外,依json-default的定义来看,方法2中还应该再加一个json的interceptor定义才比较合适。

 

--------------------------------------------------------------------------

struts.xml中的<package name="default" namespace="/" extends="struts-default">   改为 <package name="default" namespace="/" extends="json-default">

或是增加一个package标签

 

相关文章
|
JSON 文字识别 API
ocr表格识别返回的json结果,转成excel,这个转化有对应的逻辑代码吗?
ocr表格识别返回的json结果,转成excel,这个转化有对应的逻辑代码吗?
641 0
|
10月前
|
XML JSON Java
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
本文介绍了如何使用IntelliJ IDEA和Maven搭建一个整合了Struts2、Spring4、Hibernate4的J2EE项目,并配置了项目目录结构、web.xml、welcome.jsp以及多个JSP页面,用于刷新和学习传统的SSH框架。
367 0
使用IDEA+Maven搭建整合一个Struts2+Spring4+Hibernate4项目,混合使用传统Xml与@注解,返回JSP视图或JSON数据,快来给你的SSH老项目翻新一下吧
|
10月前
|
JSON 前端开发 JavaScript
JSON parse error: Cannot deserialize value of type `java.lang.Integer` from Boolean value
这篇文章讨论了前端Vue应用向后端Spring Boot服务传输数据时发生的类型不匹配问题,即后端期望接收的字段类型为`int`,而前端实际传输的类型为`Boolean`,导致无法反序列化的问题,并提供了问题的诊断和解决方案。
JSON parse error: Cannot deserialize value of type `java.lang.Integer` from Boolean value
|
10月前
|
JSON 数据格式 Python
【python】解决json.dump(字典)时报错Object of type ‘float32‘ is not JSON serializable
在使用json.dump时遇到的“Object of type ‘float32’ is not JSON serializable”错误的方法,通过自定义一个JSON编码器类来处理NumPy类型的数据。
427 1
|
10月前
|
JSON 数据格式 Java
化繁为简的魔法:Struts 2 与 JSON 联手打造超流畅数据交换体验,让应用飞起来!
【8月更文挑战第31天】在现代 Web 开发中,JSON 成为数据交换的主流格式,以其轻量、易读和易解析的特点受到青睐。Struts 2 内置对 JSON 的支持,结合 Jackson 库可便捷实现数据传输。本文通过具体示例展示了如何在 Struts 2 中进行 JSON 数据的序列化与反序列化,并结合 AJAX 技术提升 Web 应用的响应速度和用户体验。
226 0
|
11月前
|
JSON 前端开发 数据格式
【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
211 0
|
11月前
|
JSON 数据格式
Unsupported Media Type,传入的字符串数据:这里应该是Json
Unsupported Media Type,传入的字符串数据:这里应该是Json
|
11月前
|
JSON 数据格式
Content type ‘text/plain;charset=UTF-8‘ not supported,这里要把测试文件转为json格式
Content type ‘text/plain;charset=UTF-8‘ not supported,这里要把测试文件转为json格式
Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.
Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension.
|
JSON 数据格式
gson自定义Type解析json数组字符串
gson自定义Type解析json数组字符串
153 0