Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't set Struts2 result type to json

Tags:

json

struts2

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.

like image 954
Sefler Avatar asked May 27 '09 15:05

Sefler


2 Answers

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>
like image 191
Edward Castaño Avatar answered Sep 30 '22 18:09

Edward Castaño


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>
like image 24
lich0079 Avatar answered Sep 30 '22 18:09

lich0079