struts2整合json要注意的问题

昨天struts2整合json,一直出错:

There is no Action mapped for namespace / and action name ...

HTTP Status 404 - There is no Action mapped for action name...

发现我已经在struts.xml中继承了json-default了啊,后来发现原来是由于没有引入对应的包,在此做下总结。


1、加入对应的包


导入commons-beanutils-1.7.0.jar、ezmorph-1.0.3.jar、json-lib-2.1-jdk15.jar、struts2-json-plugin-2.2.3.jar四个包,这些包在Struts的lib文件夹下都能够找到。


2、要继承json-default

<package name="struts2" extends="json-default" namespace="/">


json-default事实上也是终于继承struts-default的 json-plugn中struts-plugn.xml源代码例如以下

<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>


这里你能够看到这个json-default是干了什么事情,无非就是:

  • 加了个名为json的result类型
  • 加了一层名为json的拦截器


配置完后,就能够使用Struts+json啦!

原文地址:https://www.cnblogs.com/mengfanrong/p/3831219.html