当前位置: 代码迷 >> JavaScript >> struts2跟json的整合
  详细解决方案

struts2跟json的整合

热度:434   发布时间:2013-09-26 10:32:35.0
struts2和json的整合

一.struts 2 配置action json格式 是启动tomcat异常

异常信息:

严重: Exception starting filter struts2
Unable to load configuration. - [unknown location]
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:483)
at org.apache.struts2.dispatcher.ng.InitOperations.initDispatcher(InitOperations.java:74)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.init(StrutsPrepareAndExecuteFilter.java:51)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:278)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:259)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:383)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:104)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4650)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1276)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1440)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:301)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1374)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Thread.java:619)
Caused by: Unable to load configuration. - [unknown location]
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:71)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:429)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:471)
... 19 more
Caused by: Parent package is not defined: json-default - [unknown location]
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildPackageContext(XmlConfigurationProvider.java:660)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:508)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:290)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:239)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:68)
... 21 more

解决办法:加入一个struts2-json-plugin-2.3.7.jar包即可;

二.struts2整合json出现no result type defined for type 'json'的解决方法

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定义才比较合适。



  相关解决方案