As of struts 2.1.8, you have (well can, but it's recommended) use the struts2-json-plugin rather than the googlecode version (the google code one has been transferred to apache as a struts 2 plugin).

To do that, just use the class name org.apache.struts2.json.JSONResult (see http://struts.apache.org/2.1.8.1/struts2-plugins/struts2-json-plugin/apidocs/org/apache/struts2/json/JSONResult.html ) for your json result.

Ie :

<package name="package_name" extends="struts-default" 
namespace="/namespace_name">
    <result-types>
        <result-type name="json" class="org.apache.struts2.json.JSONResult" />
    </result-types>

And include the jar file (can be found at 
http://repo2.maven.org/maven2/org/apache/struts/struts2-json-plugin/2.1.8.1/struts2-json-plugin-2.1.8.1.jar
 or in the complete distribution of struts 2) in your classpath.

So your understanding is good, but you need the struts2-json-plugin-2.1.8.1.jar 
file too.

Denis.



Le 2010-04-16 10:00, sharath karnati a écrit :
Hi sameer,
In struts 2.1.8.1 having 'struts2-dojo-plugin-2.1.8.1.jar' file so we don't need to use 'jsonplugin-0.34.jar' for using json. This is my understanding :) If we are not including 'jsonplugin-0.34.jar' then we cannot use "com.googlecode.jsonplugin.JSONResult" With new Struts 2.1.8.1 version how to use json without using any annotations in action. Can someone please send me some examples for using json in this new version of struts2. Thanks in advance. Regards, Sharath. --- On Thu, 4/15/10, hisameer<cool_sameer_fo...@yahoo.com> wrote:


From: hisameer<cool_sameer_fo...@yahoo.com>
Subject: Re: Using json in Struts 2.1.8.1
To: user@struts.apache.org
Date: Thursday, April 15, 2010, 7:22 PM



Do something like this:

In your struts.xml define result types as follows:

<package name="package_name" extends="struts-default"
namespace="/namespace_name">
         <result-types>
             <result-type name="json" class="com.googlecode.jsonplugin.JSONResult" 
/>
         </result-types>

then in the action mapping:

<action name="action_name" class="AutocompleteField "
             method="getCities()">
             <result type="json">
             </result>
</action>

and put annotation in the getter method in your action class:
@JSON
public Map<String,String>  getJson() {
         return json;
     }


and u can use this json map in the jsp page.

I Hope it helps!


sharath wrote:
Hi All,
I'm having following Action ----------------- Action(Begin) -----------------
public class AutocompleteField extends ActionSupport {

     private String city;
     private Map<String,String>  json;

     public String execute() throws Exception() {
         return SUCCESS;
     }

     public String getCities() throws Exception() {
         json = new HashMap<String,String>();

         if(city != null&&  city.length()>  0) {
             CityDao dao = new CityDao();
             List<City>  cities = dao.getCitiesStartingWith(city);
             for(City city : cities) {
                 json.put(city.getId(), city.getName() + ", " +
city.getState().getAbbreviation());
             }
         }

         return SUCCESS;
     }

     public void setCity(String city) {
         this.city = city;
     }

     public Map<String,String>  getJson() {
         return json;
     }

}

-----------------  Action(End)     -----------------
In struts.xml file I given following entries

<action name="Autocomplete!*" class="gov.action.AutocompleteField"
method="{1}">
<!--<result type="json">json</result>  -->
<result name="success" type="json" />
</action>
When I keep 'jsonplugin-0.34.jar' file getting below error java.lang.NoClassDefFoundError: com/opensymphony/xwork2/util/TextUtils After reading some posts, they said that we should not include this .jar
in this new struts2.1.8 version. I removed this .jar file
I added 'struts2-dojo-plugin-2.1.8.1.jar' file in /WEB-INF/lib folder. When I deploy the application getting below error There is no result type defined for type 'json' mapped with name
'success'.  Did you mean 'json'? - result
Can anyone please let me know, how to define 'json' in struts.xml file?
in package level I even added 'json-default'

<package name="admin" extends="struts-default,json-default">
Thanks in advance. Regards,
Sharath.







---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to