Sefler
Sefler

Reputation: 2237

can't set Struts2 result type to json

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.

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

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

Upvotes: 7

Views: 30309

Answers (5)

Maxx Selva K
Maxx Selva K

Reputation: 512

Include json-default in the extends parameter:

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

Upvotes: 0

Here is my configuration in 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" :

<result type="json"/>

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

Upvotes: 0

lich0079
lich0079

Reputation: 121

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

Upvotes: 12

rado
rado

Reputation: 4095

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


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

Upvotes: 0

Peter Kelley
Peter Kelley

Reputation: 2360

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:

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

Upvotes: 16

Related Questions