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

-- 
View this message in context: 
http://old.nabble.com/Using-json-in-Struts-2.1.8.1-tp28245651p28261689.html
Sent from the Struts - User mailing list archive at Nabble.com.


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

Reply via email to