无法将 Struts2 结果类型设置为 json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/916285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:15:35  来源:igfitidea点击:

can't set Struts2 result type to json

jsonstruts2

提问by Sefler

I want to use json with Struts2. However, when I set the action return type to "json", I got "there is no result type defined for type 'json' mapped with name 'success'." Bellow is struts.xml file.

我想在 Struts2 中使用 json。但是,当我将操作返回类型设置为“json”时,我得到“没有为名称为‘success’映射的‘json’类型定义的结果类型。” 下面是 struts.xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.custom.i18n.resources" value="resource"/>

    <package extends="struts-default" name="test">
        <action name="inputHandler" class="inputHandlerAction">
            <result name="input">/index.jsp</result>
            <result>/result.jsp</result>
        </action>
        <action name="setLangHandler" class="com.sesoft.test.setLanguageHandler">
            <result>/index.jsp</result>
        </action>

        <action name="Handler" class="com.sesoft.test.Handler">
            <result>/test2.jsp</result>
        </action>   
    </package>

    <package name="example" extends="json-default">

        <action name="ajaxHandler" class="com.sesoft.test.AjaxHandler">
            <result name="success" type="json" />
        </action>

    </package>
</struts>

Before I added the json Action, all other action performs fine. But after I added the json Action, the server failed to action with error code 503.

在我添加 json 操作之前,所有其他操作都可以正常执行。但是在我添加了 json Action 后,服务器无法操作,错误代码为 503。

libs I've added "jsonplugin-0.33.jar" to the lib directory.

libs 我已将“jsonplugin-0.33.jar”添加到 lib 目录中。

回答by Peter Kelley

You don't have the JSON result defined in your struts.xml package. If you only need default things then you can just extend json-default instead of struts-default. If you need to customise the package then include the following and that should do the trick:

您没有在 struts.xml 包中定义 JSON 结果。如果你只需要默认的东西,那么你可以扩展 json-default 而不是 struts-default。如果您需要自定义包,请包含以下内容,这应该可以解决问题:

    <result-types>
        <result-type name="json" class="com.googlecode.jsonplugin.JSONResult"/>
    </result-types>

回答by lich0079

you package should extends 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>

回答by Eduardo Martín Vega

Here is my configuration in pom.xml:

这是我的配置pom.xml

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-core</artifactId>
    <version>2.3.1.2</version>
</dependency>

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-json-plugin</artifactId>
    <version>2.3.1.2</version>
</dependency>

In the action result you only need to specify type="json":

在操作结果中,您只需要指定type="json"

<result type="json"/>

Remember the variable getter and setter in type="json"response give getters in the action.

记住变量 getter 和 settertype="json"响应在动作中给出 getter。

回答by Maxx Selva K

Include json-defaultin the extendsparameter:

包含json-defaultextends参数中:

<package name="default" extends="struts-default,json-default">
   <action> 
       ...
       ...
  </action>
</package>

回答by rado

If using Maven you may need to add the dependency e.g.

如果使用 Maven,您可能需要添加依赖项,例如


        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.2.3</version>
        </dependency>